简体   繁体   English

如何使用自动生成的值制作复合主键,并使用休眠和Spring MVC制作一个外键

[英]How make a composite primary key using auto generated value and one foreign key using hibernate and spring MVC

I am developing a web application that customer call to a employee and he put a customer's order to the system. 我正在开发一个Web应用程序,客户可以呼叫员工,然后他将客户的订单下达到系统中。

From my ER diagram , the OrderDetail class contain a composite primary key that orderId and productId . 从我的ER diagramOrderDetail类包含一个包含orderIdproductId的复合主键。 Also it contain orderedQuentity column. 它还包含orderedQuentity列。 What I want is when customer place a order it has to have what products he/she ordered and the quantity of each product he/she order and who place the order(the employee)..... 我要的是客户下订单时必须有他/她订购的产品以及他/她订购的每种产品的数量以及下订单的人(员工).....

But I feel that in my way of doing that, the orderDetail class can only contain list of product , not the quantity of each product. 但是我觉得这样做的时候, orderDetail类只能包含产品列表,而不能包含每个产品的数量。

How do I do what I want ?? 我该怎么办?

Here is my entity classes that I try to implement. 这是我尝试实现的实体类。

Customer Class : 客户类别:

@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;

}

Product Class : 产品类别:

@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;

} }

Order Class : 订单类别:

@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;
}

Employee Class : 员工类别:

@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;
}

From Customer to OrderDetail you have a OneToMany relation, I would suggest you to create a JoinTable 从Customer到OrderDetail,您有一个OneToMany关系,建议您创建一个JoinTable

So the customer entity will look like: 因此,客户实体将如下所示:

@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;

}

The same situation in Employee entity: 员工实体中的相同情况:

@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;

} }

And Product entity: 和产品实体:

@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;

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

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