@OneToMany 与复合键的关系

发布于 2025-01-02 22:11:33 字数 1184 浏览 0 评论 0原文

我有以下模型(忽略不相关/明显的代码,例如设置器和获取器):

@Entity
public class Invoice {
    @Id // sequence generated
    private Long invoiceId;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(name = "invoiceId", referencedColumnName = "invoiceId")
    @OrderBy("id.itemNumber")
    List<Item> items = new ArrayList<Item>();
}


@Entity
public class Item {
    @EmbeddedId
    private ItemPK id;
}


@Embeddable
public class ItemPK {
    private Long invoiceId;
    private Long itemNumber;
}

无法更改数据库结构,因为它是遗留数据库:(

itemNumber 基本上是从零开始的索引。

如果我采用现有的发票只需删除现有项目并在列表中添加 2 个新项目,当尝试更新发票对象时,我会遇到各种问题:

如果我将 ItemPK 保留为空,我会得到: org.hibernate.TransientObjectException:对象是一个未保存的瞬态实例 - 在合并之前保存瞬态实例

如果我尝试自己填写 ItemPK 的值(发票的 ID 和索引),我得到:org.hibernate.NonUniqueObjectException:具有相同标识符值的不同对象已与会话关联(数据库和它具有发票 ID 且索引 = 0,就像我的新项目列表中的第一个对象一样)。

如果我尝试将发票Id 保留为空(在 ItemPK 对象中),则会得到:java.sql.BatchUpdateException: ORA-01400: 无法将 NULL 插入 ("Item"."invoiceId")

有没有办法让 Hibernate/JPA 处理这个问题? 或者我是否需要完全删除关联,将列表定义为 @Transient 并填充它并将其保存在我的服务类中?

谢谢。

I have the following model (ignoring irrelevant/obvious code, like setters and getters):

@Entity
public class Invoice {
    @Id // sequence generated
    private Long invoiceId;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(name = "invoiceId", referencedColumnName = "invoiceId")
    @OrderBy("id.itemNumber")
    List<Item> items = new ArrayList<Item>();
}


@Entity
public class Item {
    @EmbeddedId
    private ItemPK id;
}


@Embeddable
public class ItemPK {
    private Long invoiceId;
    private Long itemNumber;
}

Can't change the DB structure, as it's a legacy DB :(

The itemNumber is basically an index that starts at zero.

If I take an existing Invoice and just remove the existing item and add 2 new items in the list, when trying to update the Invoice object I run into all sorts of problems:

If I leave the ItemPK null, I get:
org.hibernate.TransientObjectException: object is an unsaved transient instance - save the transient instance before merging

If I try to fill the values of ItemPK myself (the id of the invoice and the index), I get: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session (there already is an invoice item in the DB and it has the invoice id and the index = 0, just like the first object in my new list of items).

If I try to leave the invoiceId null (in the ItemPK object), I get: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("Item"."invoiceId")

Is there anyway to let Hibernate/JPA handle this ?
Or do I need to completely remove the association, defined the list as @Transient and just fill it and save it in my service classes ?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2025-01-09 22:11:33

这里实际上有一个双向关联,因为 Item 实体中的 InvoiceId 实际上是 Invoice 的外键。因此,您应该将关联映射为双向关联,并使用 < code>@MapsId 注释。它的javadoc有一个例子。

You effectively have a bidirectional association here, since the invoiceId in the Item entity is in fact a foreign key to the Invoice. You should thus map the association as a bidirectional one, and use the @MapsId annotation. Its javadoc has an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文