Hibernate、Persistence 以及 @OneToMany 和 @ManyToOne 注解问题

发布于 2024-09-14 08:11:28 字数 1063 浏览 6 评论 0原文

我对 @OneToMany 和 @ManyToOne 注释有一些问题。

我有两个类 Suite 和 SuiteVersion。 SuiteVersion 依赖于套件。所以我在我的代码中实现了这个:

Class Suite:

@OneToMany(mappedBy = "suite")
@Cascade(CascadeType.DELETE_ORPHAN)
private List<SuiteVersion> listSuiteVersion = new ArrayList<SuiteVersion>();

Class SuiteVersion:

@ManyToOne()
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
private Suite suite;

但是当我删除与 SuiteVersion 关联的 Suite 时,我遇到了一些问题。 Hibernate 不会在 Suite 之前删除 SuiteVersion。我不知道为什么,因为我在代码中提到了这一点:

@Cascade(CascadeType.DELETE_ORPHAN)

这是我删除套件时获得的日志:

Hibernate:从 ID_SUITE= 处的 SUITE 中删除? 2010 年 8 月 13 日 09:40:50 org.hibernate.util.JDBCExceptionReporter logExceptions 注意:SQL 错误:-8,SQLState:23504 2010 年 8 月 13 日 09:40:50 org.hibernate.util.JDBCExceptionReporter logExceptions GRAVE:违反完整性约束:外键无动作; FK42895651EA304E6 表:SUITE_VERSION

预先感谢您的帮助。

最好的问候,

弗洛伦特,

PS:对不起我的英语,我是法国人。

I have some problem with @OneToMany and @ManyToOne annotations.

I have two class Suite and SuiteVersion. A SuiteVersion is dependent of a suite. So i have implemented this in my code:

Class Suite :

@OneToMany(mappedBy = "suite")
@Cascade(CascadeType.DELETE_ORPHAN)
private List<SuiteVersion> listSuiteVersion = new ArrayList<SuiteVersion>();

Class SuiteVersion :

@ManyToOne()
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
private Suite suite;

But i have some problem when i delete a Suite whose have SuiteVersion associated.
Hibernate don't delete SuiteVersion before Suite.I don't know why because i have mentioned this in my code :

@Cascade(CascadeType.DELETE_ORPHAN)

This the log i obtained when i delete suite :

Hibernate: delete from SUITE where ID_SUITE=?
13 août 2010 09:40:50 org.hibernate.util.JDBCExceptionReporter logExceptions
ATTENTION: SQL Error: -8, SQLState: 23504
13 août 2010 09:40:50 org.hibernate.util.JDBCExceptionReporter logExceptions
GRAVE: integrity constraint violation: foreign key no action; FK42895651EA304E6 table: SUITE_VERSION

Thank you in advance for your help.

Best Regards,

Florent,

P.S : Sorry for my english i'm french.

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

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

发布评论

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

评论(1

打小就很酷 2024-09-21 08:11:28

但是当我删除与 SuiteVersion 关联的套件时,我遇到了一些问题。 Hibernate 不会删除 Suite 之前的 SuiteVersion

这是因为您没有REMOVE 操作从 Suite 级联到 SuiteVersion。为了获得所需的结果,您需要类似这样的内容(假设您使用的是 JPA 1.0):

@OneToMany(mappedBy = "suite", cascade = javax.persistence.CascadeType.REMOVE)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private List<SuiteVersion> listSuiteVersion = new ArrayList<SuiteVersion>();

我在此处使用完全限定名称来清楚地显示 Hibernate 特定和标准 JPA 注释。

Hibernate 特定的 CascadeType.DELETE_ORPHAN 是另外一回事,它用于告诉 Hibernate 删除特定的 SuiteVersion 如果您从集合中删除它(如果没有它,SuiteVersion 记录将被更新以删除到父 Suite 的链接,但仍保留在表中,因此成为“孤儿”。

请注意,如果您使用 JPA 2.0(即 Hibernate 3.5+),现在有一种处理孤立记录的标准方法,即不使用 Hibernate 特定注释。您可以在 OneToMany(和 OneToOne)关联中指定 orphanRemoval 选项。像这样:

@OneToMany(mappedBy = "suite", cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<SuiteVersion> listSuiteVersion = new ArrayList<SuiteVersion>();

但这超出了这个问题,因为这实际上不是您正在寻找的。

But i have some problem when i delete a Suite whose have SuiteVersion associated. Hibernate don't delete SuiteVersion before Suite.

That's because you're NOT cascading the REMOVE operation from Suite to SuiteVersion. To obtain the desired result, you need something like this (assuming you're using JPA 1.0):

@OneToMany(mappedBy = "suite", cascade = javax.persistence.CascadeType.REMOVE)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private List<SuiteVersion> listSuiteVersion = new ArrayList<SuiteVersion>();

I used fully qualified names to clearly show the Hibernate specific and the standard JPA annotations here.

The Hibernate specific CascadeType.DELETE_ORPHAN is something else, it is used to tell Hibernate to delete a specific SuiteVersion if you remove it from the collection (without it, the SuiteVersion record would be updated to remove the link to the parent Suite but would remain in the table, being thus an "orphan").

Note that if you are using JPA 2.0 (i.e. Hibernate 3.5+), there is now a standard way to deal with orphan records i.e. without using the Hibernate specific annotation. You can specify an orphanRemoval option in your OneToMany (and OneToOne) association. Like this:

@OneToMany(mappedBy = "suite", cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<SuiteVersion> listSuiteVersion = new ArrayList<SuiteVersion>();

But this goes beyond this question as this is actually not what you're looking for.

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