Hibernate、Persistence 以及 @OneToMany 和 @ManyToOne 注解问题
我对 @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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您没有将
REMOVE
操作从Suite
级联到SuiteVersion
。为了获得所需的结果,您需要类似这样的内容(假设您使用的是 JPA 1.0):我在此处使用完全限定名称来清楚地显示 Hibernate 特定和标准 JPA 注释。
Hibernate 特定的
CascadeType.DELETE_ORPHAN
是另外一回事,它用于告诉 Hibernate 删除特定的SuiteVersion
如果您从集合中删除它(如果没有它,SuiteVersion
记录将被更新以删除到父Suite
的链接,但仍保留在表中,因此成为“孤儿”。请注意,如果您使用 JPA 2.0(即 Hibernate 3.5+),现在有一种处理孤立记录的标准方法,即不使用 Hibernate 特定注释。您可以在
OneToMany
(和OneToOne
)关联中指定orphanRemoval
选项。像这样:但这超出了这个问题,因为这实际上不是您正在寻找的。
That's because you're NOT cascading the
REMOVE
operation fromSuite
toSuiteVersion
. To obtain the desired result, you need something like this (assuming you're using JPA 1.0):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 specificSuiteVersion
if you remove it from the collection (without it, theSuiteVersion
record would be updated to remove the link to the parentSuite
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 yourOneToMany
(andOneToOne
) association. Like this:But this goes beyond this question as this is actually not what you're looking for.