簡體   English   中英

Spring Data JPA-ManyToMany-JPQL-存儲庫中的@Query形式

[英]Spring Data JPA - ManyToMany - JPQL - @Query formation in Repository

我有一個使用PostgreSQL RDBMS的spring boot項目。 我在兩個實體之間有@ManyToMany關系 - 客戶和產品。 它們由customer_product加入。 但是在存儲庫層形成JPQL時,我遇到了困難。 這是實體:

@Entity
@NamedQuery(name="Customer.findAll", query="SELECT c FROM Customer c")
public class Customer implements Serializable {

... all properties ... getter setter constructor...

//bi-directional many-to-many association to Product
    @JsonIgnore
    @ManyToMany
    @JoinTable(
        name="customer_product"
        , joinColumns={
            @JoinColumn(name="customer_id")
            }
        , inverseJoinColumns={
            @JoinColumn(name="product_id")
            }
        )
    private List<Product> products;

@Entity
@NamedQuery(name="Product.findAll", query="SELECT p FROM Product p")
public class Product implements Serializable {
... all properties ... getter setter ... constructors ...

//bi-directional many-to-many association to Customer
    @JsonIgnore
    @ManyToMany(mappedBy="products")
    private List<Customer> customers;

現在在以后的存儲庫中:

@Repository
public interface CustomerRepository extends CrudRepository<Customer, Integer> {

//Below mentioned query works perfectly as Customer & Address has OneToMany Relation

@Query("select new com.arindam.springjpa.springdatajpaexamples.vo.CustomerDetailsVO(c.id, c.customerName, a.fullAddress) from Customer c, Address a where c.address.addressId=a.addressId")
    List<CustomerDetailsVO> findAllCustomerDetails();

// But I need help on below query
// I want to find out full details of ManyToMany relation between customer & product

@Query("select new com.arindam.springjpa.springdatajpaexamples.vo.CustomerProductAddressDetailsVO(c.id, c.customerName, a.fullAddress, p.productName) from Customer c, Address a, Product p where c.address.addressId=a.addressId and c.products.product.productId=p.productId")

List<CustomerProductAddressDetailsVO> findAllCustomerAddressProductDetails();

要在VO中獲得結果是簡單的VO類

@Entity
public class CustomerProductAddressDetailsVO {

    private Integer id;
    private String customerName;
    private String fullAddress;
    private String productName;

//Like a simple POJO with getter setter constructors

你能建議嗎?

感謝您的寶貴意見。

我已經解決了這個問題。 由於實體類是使用Eclipse插件生成的,因此尚未生成customer_product的類。 所以我手動生成它並使用查詢。 所以最終的代碼是:

@Entity
@Table(name="customer_product")
@NamedQuery(name="CustomerProduct.findAll", query="SELECT c FROM CustomerProduct c")
public class CustomerProduct implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private Integer id;

    @Column(name="customer_id")
    private Integer customerId;

    @Column(name="product_id")
    private Integer productId;

和存儲庫層:

@Query("select new com.arindam.springjpa.springdatajpaexamples.vo.CustomerProductAddressDetailsVO(cp.id, c.customerName, a.fullAddress, p.productName) from Customer c, Address a, Product p, CustomerProduct cp "
            + "where c.address.addressId=a.addressId and cp.productId=p.productId and cp.customerId=c.id")
    List<CustomerProductAddressDetailsVO> findAllCustomerAddressProductDetails();

它完美地運作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM