Hibernate的新功能,我正在尝试完成一个非常简单的任务,但由于错误的数量而绕圈而行。
具有一对多关系的两个表Cart
和Items
。我想在父表实体上调用save,并希望子表也得到保存。
脚本1:
CREATE TABLE cart
(
cart_id INTEGER NOT NULL,
reference_number INTEGER,
PRIMARY KEY (cart_id)
);
CREATE TABLE item
(
item_id INTEGER NOT NULL,
cart_id INTEGER NOT NULL,
name Varchar(255) ,
description Varchar(255) ,
PRIMARY KEY (item_id),
FOREIGN KEY (cart_id) REFERENCES cart
);
下面是使用Entity类的休眠代码生成方法。
@Entity
@Table(name = "CART", schema = "MYSCHEMA")
public class Cart implements java.io.Serializable {
private int cartId;
private Integer referenceNumber;
private List<Item> items = new ArrayList<Item>();
@Id
@Column(name = "CART_ID", unique = true, nullable = false)
public int getCartId() {
return this.cartId;
}
@OneToMany(mappedBy = "cart", cascade = CascadeType.ALL, orphanRemoval = true)
public List<Item> getItems() {
return items;
}
public void addItem(Item item) {
items.add(item);
item.setCart(this);
}
public void removeComment(Item item) {
items.remove(item);
item.setCart(this);
}
public void setItems(List<Item> items) {
this.items = items;
}
// Excluding rest of the Getters and Setterss
}
@Entity
@Table(name = "ITEM", schema = "MYSCHEMA")
public class Item implements java.io.Serializable {
private int itemId;
private int cartId;
private String name;
private String description;
private Cart cart;
@Id
@Column(name = "ITEM_ID", unique = true, nullable = false)
public int getItemId() {
return this.itemId;
}
@ManyToOne
@JoinColumn(name = "cart_id", nullable = false, insertable = false, updatable = false)
public Cart getCart() {
return cart;
}
public void setCart(Cart cart) {
cart = cart;
}
// Excluding rest of the Getters and Setterss
}
使用此设置时,可以跟随运行。
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Cart cart = new Cart();
cart.setReferenceNumber(333);
Item item1 = new Item();
item1.setName("itemONe");
item1.setDescription("itemOneDescription");
item1.setCart(cart);
cart.addItem(item1);
Item item2 = new Item();
item2.setName("itemONe");
item2.setDescription("itemOneDescription");
item2.setCart(cart);
cart.addItem(item2);
session.save(cart);
session.getTransaction().commit();
它给出以下错误。
Exception in thread "main" org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [com.testHibernate.models.Item#0]
由于Hibernate和Database都不在处理主键的自动生成,因此具有意义。因此,如果仅使用
Item1
运行以上代码,则它可以工作并保存两条记录。如果我将数据库更改为以下内容,则通过数据库生成ID
脚本2:
CREATE TABLE cart
(
cart_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
reference_number INTEGER,
PRIMARY KEY (cart_id)
);
CREATE TABLE item
(
item_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
cart_id INTEGER NOT NULL,
name Varchar(255) ,
description Varchar(255) ,
PRIMARY KEY (item_id),
FOREIGN KEY (cart_id) REFERENCES cart
);
保持Entity类与上面显示的相同,它无效。出现相同的错误。
我尝试在两个类中都将
@GeneratedValue(strategy = GenerationType.IDENTITY)
与PrimeryKey字段一起使用,并且保存时显示以下错误。ERROR: INSERT on table 'ITEM' caused a violation of foreign key constraint 'SQL0000000002-06ba0cd1-016e-2cf2-dad9-ffff908894b5' for key (0). The statement has been rolled back.
Hibernate是否应该使用
cascade = CascadeType.ALL
处理呢?我想念什么?
休眠v5.4.4
Derby DB v10.15.1.3
最佳答案
在第一种情况下,您具有2个具有相同ID的项目实体-零。
对于第二个错误,将联接列定义为cart_id
时,如何设置不可为空的外键列insertable = false, updatable = false
?删除这些属性,它将起作用。
关于hibernate - 使用DerbyDB进行一对多双向关系的Hibernate注释映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58868349/