我在MySQL数据库中有三个表
1.使用主键的表用户:用户名。
2.具有主键的表顺序:orderid和外键:username(参考表用户的用户名)
3.表orderdetail与主键:id和复合外键:orderid;用户名(请参阅订单中的orderid和用户名。)
现在如何为Table ordersDetail设置Entity类OrderDetail,让hibernate正常工作。
//订单类
@Entity
@Table(name="orders")
public class Order {
@Id
private long orderid;
private double price;
private String status;
@ManyToOne
@JoinColumn(name="username")
private User user;
//Constructor take 0 args and 4 args.
public Order() {
this.user = new User();
}
public Order(long orderid, double price, String status, User user) {
this.orderid = orderid;
this.price = price;
this.status = status;
this.user = user;
}
@Embeddable
class CompositeKey implements Serializable {
private static final long serialVersionUID = -8711530222180590954L;
public String username;
public long orderid;
}
// OrderDetail Class
@Entity
@Table(name = "ordersdetail")
public class OrderDetail implements Serializable{
private static final long serialVersionUID = -3936223721422733989L;
@Id
@GeneratedValue
private int id;
private int quantity;
private Order order;
@ManyToOne
@JoinColumn(name = "goodsid")
private Goods goods;
@ManyToOne
@JoinColumns({
@JoinColumn(name = "username", referencedColumnName = "username"),
@JoinColumn(name = "orderid", referencedColumnName = "orderid") })
private CompositeKey key;
public CompositeKey getKey() {
return key;
}
public void setKey(CompositeKey key) {
this.key = key;
}