gpt4 book ai didi

java - 复合主键 Hibernate 和 Java 实现

转载 作者:行者123 更新时间:2023-11-30 07:34:20 25 4
gpt4 key购买 nike

我定义了一个非常简单的数据库如下:

MySql Simple Database

我正在使用 hibernate 插件生成不同的类,包括客户和订单之间的一对多关系。我将这种关系作为一种识别关系,因为如果没有链接到客户,订单就不会存在。

我有以下三个类:

=> 类客户

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

=> 类(class)顺序:

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 对象没有做任何花哨的事情......只是获取 session 的常用 HibernateUtil 实现,最后调用“保存”方法。我的问题如下。我以前从未处理过复合键...请原谅我的愚蠢问题。我不想手动处理不同的 ID 和主键。对于 Cutomer 类,这不是问题,因为我设置了一个在数据库中保存时自动创建 id 的策略。对于 Order 类,我也不想处理它。但是由于 key 是复合的,Hibernate 在尝试将此对象保存在 DB 中时会生成一个标识符 GenerationExeption ...期望 hibernate 将客户 ID 链接到复合键中的客户 ID...做这件事的正确方法是什么?

谢谢

最佳答案

为什么要在订单实体的主键中包含客户 ID 外键?它的主键应该是idOrderCustomer_idCustomer 应该是一个外键,不应该是订单 ID 的一部分。

关于java - 复合主键 Hibernate 和 Java 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5231875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com