繁体   English   中英

如何与具有Hibernate,JPA的复合键的不同实体建立一对多的关系?

[英]How to build one to many relationship with different entities which have a composite key with Hibernate, JPA?

我想为以下关系构建实体类。 我想要一个具有复合键的实体ProductWiseCustomer。 这些密钥还映射到产品和客户实体。 如何达到目的?

实体关系

到目前为止我所做的一切。

Product.java

    @Entity
    @Table(name = "product")
    public class Product {
        @Id
        private Long productId;
        private String productName;
        private Decimal productPrice;

        @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = CustomerProductCompound.class)
        private Set<CustomerProductCompound> customerProductCompound;

        //Constructor
        //Setter-getter
    }

Customer.java

    @Entity
    @Table(name = "customerinfo")
    public class CustomerInfo {
        @Id

        private Long customerId;
        private String customerName;
        private Boolean isActive;

        @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = CustomerProductCompound.class)
        private Set<CustomerProductCompound> customerProductCompound;

   //Constructor
   //Setter-getter
}

CustomerProductCompound.java

    @Embeddable
    public class CustomerProductCompound
   {

        @ManyToOne
        @JoinColumn(name = "customerId")
        private CustomerInfo customerInfo;

        @ManyToOne
        @JoinColumn(name = "productId")
        private Product product;

        //Constructor
        //Setter-getter
    }

运行应用程序时收到以下错误:

使用@OneToMany或@ManyToMany定位未映射的类:com.auth.model.CustomerInfo.customerProductCompound [com.auth.model.CustomerProductCompound]。

一种解决方案是使用带有@EmbeddableId的复合标识符。

@Entity
public class ProductWiseCustomer {
    @EmbeddedId
    private ProductCustomerKey key;

}

@Embeddable
public class ProductCustomerKey {

    @ManyToOne(fetch = FetchType.LAZY)
    private Customer customer;

    @ManyToOne(fetch = FetchType.LAZY)
    private Product product;
}

请参阅hibernate文档:

https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#identifiers-composite-aggregated

CustomerProductCompound因为您只定义了ProductWiseCustomer的主键。 CustomerInfoProduct集合必须包含ProductWiseCustomer项,而不是其密钥。

@Entity
@Table(name = "product")
public class Product {
    @Id
    private Long productId;
    private String productName;
    private Decimal productPrice;

    @OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    private Set<ProductWiseCustomer> productWiseCustomers;

}

@Entity
@Table(name = "customerinfo")
public class CustomerInfo {
    @Id

    private Long customerId;
    private String customerName;
    private Boolean isActive;

    @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    private Set<ProductWiseCustomer> productWiseCustomers;

}

注意我在注释中添加了mappedBy属性。 它需要指向引用此对象的另一侧的属性名称。 JPA名称,而不是SQL名称。 targetEntity很少是必需的,我建议使用orphanRemoval ,这样如果你从集合中删除了一个,你就不必手动删除它以使其消失。

对于ProductWiseCustomer ,您需要与Modular Coder所示相同的密钥

@Embeddable
public class ProductCustomerKey {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "customerId)
    private Customer customer;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "productId")
    private Product product;
}

但我建议您使用@IdClass而不是@EmbeddedId

@Entity
@IdClass(ProductCustomerKey.class)
public class ProductWiseCustomer {
    @ManyToOne(fetch = FetchType.LAZY) // should be lazy here
    @JoinColumn(name = "customerId)
    private Customer customer;

    @ManyToOne(fetch = FetchType.LAZY) // should be lazy here
    @JoinColumn(name = "productId")
    private Product product;

    private OffsetDateTime createDate;
    private String remarks;
    // getters, setters
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM