[英]Composite Primary Key Hibernate and Java Implementation
我定义了一个非常简单的数据库,如下所示:
我正在使用休眠插件生成不同的类,包括客户和订单之间的一对多关系。 我将这种关系作为一种识别关系,因为如果不链接到客户,则订单将不存在。
我有以下三个课程:
=>类客户
private int idCustomer;
private String name;
private Set<Order> orders = new HashSet<Order>(0);
public Customer() {
}
public Customer(int idCustomer) {
this.idCustomer = idCustomer;
}
public Customer(int idCustomer, String name, Set<Order> orders) {
this.idCustomer = idCustomer;
this.name = name;
this.orders = orders;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idCustomer", unique = true, nullable = false)
public int getIdCustomer() {
return this.idCustomer;
}
public void setIdCustomer(int idCustomer) {
this.idCustomer = idCustomer;
}
@Column(name = "name", length = 45)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
public Set<Order> getOrders() {
return this.orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
=>班级顺序:
private OrderId id;
private Customer customer;
private String quantity;
private Float price;
public Order() {
}
public Order(OrderId id, Customer customer) {
this.id = id;
this.customer = customer;
}
public Order(OrderId id, Customer customer, String quantity, Float price) {
this.id = id;
this.customer = customer;
this.quantity = quantity;
this.price = price;
}
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "idOrder", column = @Column(name = "idOrder", nullable = false)),
@AttributeOverride(name = "customerIdCustomer", column = @Column(name = "Customer_idCustomer", nullable = false)) })
public OrderId getId() {
return this.id;
}
public void setId(OrderId id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Customer_idCustomer", nullable = false, insertable = false, updatable = false)
public Customer getCustomer() {
return this.customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@Column(name = "quantity", length = 45)
public String getQuantity() {
return this.quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
@Column(name = "price", precision = 12, scale = 0)
public Float getPrice() {
return this.price;
}
public void setPrice(Float price) {
this.price = price;
}
=>类OrderId
private int idOrder;
private int customerIdCustomer;
public OrderId() {
}
public OrderId(int idOrder, int customerIdCustomer) {
this.idOrder = idOrder;
this.customerIdCustomer = customerIdCustomer;
}
@Column(name = "idOrder", nullable = false)
public int getIdOrder() {
return this.idOrder;
}
public void setIdOrder(int idOrder) {
this.idOrder = idOrder;
}
@Column(name = "Customer_idCustomer", nullable = false)
public int getCustomerIdCustomer() {
return this.customerIdCustomer;
}
public void setCustomerIdCustomer(int customerIdCustomer) {
this.customerIdCustomer = customerIdCustomer;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof OrderId))
return false;
OrderId castOther = (OrderId) other;
return (this.getIdOrder() == castOther.getIdOrder())
&& (this.getCustomerIdCustomer() == castOther
.getCustomerIdCustomer());
}
public int hashCode() {
int result = 17;
result = 37 * result + this.getIdOrder();
result = 37 * result + this.getCustomerIdCustomer();
return result;
}
然后,我上了主课:
public static void main(String[] args) {
Customer customer = new Customer();
customer.setName("John Doe");
CustomerDAO.save(customer);
Order order = new Order();
order.setQuantity(10);
order.setPrice(100);
order.setCustomer(customer);
OrderDAO.save(order);
}
请注意,我的DAO对象没有做任何花哨的事情……只是获取会话并最终调用'save'方法的常规HibernateUtil实现。 我的问题如下。 我从来没有处理过复合键...事先请我提出我的愚蠢问题。 我不想手动处理不同的ID和主键。 对于Cutomer类,这不是问题,因为我设置了一个策略,该策略在保存到数据库时将自动创建id。 对于Order类,我也不想处理。 但是由于键是复合键,因此Hibernate在尝试将该对象保存到DB中时会生成一个identifierGenerationExeption ...我想要的是以下内容=>自动生成的订单ID,并且由于我在订单对象中设置了客户,所以我会期望休眠将客户ID链接到组合键中的ID。做这件事的正确方法是什么?
谢谢
不鼓励使用组合键,仅应在强制使用组合键时使用(例如:遗留数据库)。 我认为您应该使Order中的Customer_id
not null
。 该订单有一个简单的常规ID( idOrder
),所有内容都应简单明了。
为什么将客户ID外键包含在Order实体的主键中? 它的主键应该是idOrder
。 Customer_idCustomer
应该是外键,并且不应该是Order ID的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.