与 DerbyDB 的一对多双向关系的 Hibernate 注释映射

Hibernate annotation mapping for One-To-Many bidirectional relation with DerbyDB

Hibernate 的新功能,我试图完成一个非常简单的任务,但最终却陷入了错误的循环中。

两个 tables CartItems 具有一对多关系。我想在父 table 实体上调用保存并期望子 table 也被保存。

脚本 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
);

使用 hibernate 代码生成以下是实体 类。

@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 和数据库都没有处理主键的自动生成。所以,如果我 运行 上面的代码与 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
);

保持实体类如上所示,没有任何效果。出现同样的错误。

我尝试将 @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 德比数据库 v10.15.1.3

在第一种情况下,您有 2 个具有相同 ID 的项目实体 - 零。

对于第二个错误,当您将连接列定义为 insertable = false, updatable = false 时,如何设置不可空的外键列 cart_id?删除这些属性,它应该可以工作。