如何使用自动生成的值和使用hibernate和spring MVC的一个外键来创建复合主键

时间:2015-04-19 07:05:54

标签: java spring hibernate composite-primary-key

我正在开发一个客户致电给员工的Web应用程序,并将客户的订单发送给系统。

在我的ER diagram中,OrderDetail类包含orderIdproductId的复合主键。它还包含orderedQuentity列。 我想要的是当客户下订单时,他/她订购的产品和他/她订购的每件产品的数量以及下订单的人(员工)......

但我觉得以我的方式,orderDetail类只能包含产品清单,而不是每种产品的数量。

我如何做我想做的事?

这是我尝试实现的实体类。

客户类:

@Entity
public class Customer {

    @Id
    @GeneratedValue
    private Integer id;
    private String firstName;
    private String lastName;
    private String companyName;
    private Integer teleponeNumber;

    @OneToMany(mappedBy="customer")
    private List<OrderDetail> orders;

}

产品类别:

@Entity

public class Product {

    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private Integer availableQuantity;
    private String unitType;
    private Integer unitPrice;
    private String description;

    @ManyToMany(mappedBy="products")
    private List<OrderDetail> details;

}

订单类:

@Entity

public class OrderDetail {

    @Id
    @GeneratedValue
    private Integer orderId;
    private Integer orderedQuentity;    
    private String customerAddress;
    private String shipingAddress;
    private String orderStatus;

    @ManyToOne
    @JoinColumn(name="Employee_Id")
    private Employee employee;

    @ManyToOne
    @JoinColumn(name="customer_id")
    private Customer customer;

    @ManyToMany
    @JoinTable
    private List<Product> products;
}

员工类:

@Entity
public class Employee {

    @Id
    @GeneratedValue
    private Integer id;
    private String firstName;
    private String lastName;
    private String designation;
    private String email;
    private String password;

    @OneToMany(mappedBy="employee")
    private List<OrderDetail> orders;
}

1 个答案:

答案 0 :(得分:0)

从Customer到OrderDetail你有一个OneToMany关系,我建议你创建一个JoinTable

所以客户实体看起来像:

@Entity
public class Customer {

@Id
@GeneratedValue
private Integer id;
private String firstName;
private String lastName;
private String companyName;
private Integer teleponeNumber;

@OneToMany(mappedBy="customer")
@JoinTable(name = “customer_order_details”, joinColumns= { @JoinColumn(name = “customer_id”, referencedColumnName=”id”) }, inverseJoinColumns = { @JoinColumn(name = “order_id”, referencedColumnName = “id”) })
private List<OrderDetail> orders;

}

Employee实体中的相同情况:

@Entity
public class Employee {

@Id
@GeneratedValue
private Integer id;
private String firstName;
private String lastName;
private String designation;
private String email;
private String password;

@OneToMany(mappedBy="employee")
@JoinTable(name = “employee_order_details”, joinColumns= {     @JoinColumn(name = “employee_id”, referencedColumnName=”id”) }, inverseJoinColumns = { @JoinColumn(name = “order_id”, referencedColumnName = “id”) })

private List<OrderDetail> orders;

}

和产品实体:

@Entity

public class Product {

@Id
@GeneratedValue
private Integer id;
private String name;
private Integer availableQuantity;
private String unitType;
private Integer unitPrice;
private String description;

@ManyToMany(mappedBy="products")
@JoinTable(name = “product_order_details”, joinColumns= { @JoinColumn(name = “product_id”, referencedColumnName=”id”) }, inverseJoinColumns = { @JoinColumn(name = “order_id”, referencedColumnName = “id") })
private List<OrderDetail> details;