Spring Data JPA-ManyToMany-JPQL-存储库中的@Query形式

时间:2019-03-17 23:10:18

标签: java spring-boot spring-data-jpa jpql

我有一个使用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

能否请您提出建议。

感谢您的宝贵反馈。

1 个答案:

答案 0 :(得分:0)

我已经解决了问题。由于实体类是使用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();

它运行完美。