java / hibernate-如何使用注释映射三个表/实体(休眠错误未映射到单个属性)

时间:2019-07-13 15:27:41

标签: java annotations hibernate-mapping

请帮助!!有三个表

customer, order,order_details

一个客户可以有多个订单,一个订单可以有一个客户。 一个订单可以有许多订单详细信息,而一个订单详细信息可以有一个订单。

上课之后。

OrderDetail.java

@Entity
public class OrderDetail {

    @EmbeddedId
    private OrderDetail_PK orderDetail_PK;

    private int qty;
    private double unitPrice;

    @ManyToOne
    @JoinColumn(name="orderId", referencedColumnName = "id", insertable = false, updatable = false)
    private Order order;

    @ManyToOne
    @JoinColumn(name="itemCode", referencedColumnName = "code", insertable = false, updatable = false)
    private Item item;
    ...
    ..

OrderDetail_PK.java

 @Embeddable
public class OrderDetail_PK implements Serializable{

    private String orderId;
    private String itemCode;
..
..

Order.java

    @Entity
@Table(name="`Order`")
public class Order { 

    @EmbeddedId
    private Order_PK order_PK;

    @Id
    private String id;
    @Temporal(TemporalType.DATE)
    private Date date;

    @ManyToOne
    @JoinColumn(name="customerId", referencedColumnName = "customerId", insertable = false, updatable = false)
    private Customer customer;

    @OneToMany(mappedBy = "order", cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
    private List<OrderDetail> orderDetails = new ArrayList<>();
..
..

Order_PK.java

@Embeddable
public class Order_PK implements Serializable{

    private String Id;
    private String customerId;
..
..

cusotomer.java

@Entity
public class Customer {

    @Id
    @Column(name = "customerId")
    private String id;
    private String name;
    private String address;

    @OneToMany(mappedBy = "customer", cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
    private List<Order> orders = new ArrayList<>();

    @OneToMany(mappedBy = "customer", cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
    private List<Payment> payments = new ArrayList<>();
..
..

我的代码有什么问题?我需要快速帮助。

1 个答案:

答案 0 :(得分:1)

由于使用的是复合键,因此需要定义所有相关的联接列:

@Entity
public class OrderDetail {

    @ManyToOne
    @JoinColumns({
            @JoinColumn(name="orderId", referencedColumnName = "id", insertable = false, updatable = false),
            @JoinColumn(name="customerId", referencedColumnName = "id", insertable = false, updatable = false),
    })
    private Order order;