前提・実現したいこと
Spring Boot + Spring Data JPAでテーブルを結合したいです。
SQL文が間違っているのだと思いますが、悩みすぎて頭が沸騰し、正常な思考が出来なくなってしまったのでお助けください。
発生している問題・エラーメッセージ
Caused by: org.hibernate.QueryException: could not resolve property: shop_id of: com.example.pizza.entity.Customer
Caused by: org.hibernate.QueryException: could not resolve property: shop_id of: com.example.pizza.entity.Customer [SELECT p.productName, p.productPrice, p.customer.name, p.customer.address FROM com.example.pizza.entity.Purchase p INNER JOIN p.customer WHERE p.customer.shop_id = :shopId]
Purchase.java
package com.example.pizza.entity; import java.time.LocalDateTime; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import lombok.Data; @Entity @Data @Table(name="purchase") public class Purchase { @Id @Column(name="purchase_id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Long purchaseId; @ManyToOne @JoinColumn(name="shop_id") private Customer customer ; @Column(name="customer_id") private Long customerId; @Column(name="product_name") private String productName; @Column(name="product_price") private int productPrice; @Column(name="date") private LocalDateTime date; @Column(name="flag") private boolean flag; }
Customer.java
package com.example.pizza.entity; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import lombok.Data; @Entity @Data @Table(name="customer") public class Customer { @Id @Column(name="customer_id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Long customerId; @Column(name="shop_id") private Long shopId; @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL) private List<Purchase> purchase; @Column(name="name") private String name; @Column(name="phone") private String phone; @Column(name="address") private String address; }
PurchaseRepository.java
package com.example.pizza.repository; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import com.example.pizza.entity.Purchase; @Repository public interface PurchaseRepository extends JpaRepository<Purchase, Long>{ @Query("SELECT p FROM Purchase p WHERE p.shopId = :shopId") List<Purchase> findPurchases(@Param("shopId")Long shopId); @Query("SELECT p.productName, p.productPrice, p.customer.name, p.customer.address" +" FROM Purchase p INNER JOIN p.customer WHERE p.customer.shop_id = :shopId") List<Purchase> findPandC(@Param("shopId") Long shopId); }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。