Hibernate、检测和删除级联顺序
我有一个带有 JPA 连接实体的复杂对象图。当我删除父级时,删除会正确地级联到子级。
然后我检测父类(以免急切地加载一对一关系),并且在删除时我会收到引用完整性违规异常。查看刷新时的查询休眠问题,我可以看到休眠确实尝试以错误的顺序删除记录,因此数据库抱怨引用完整性并引发异常。
为什么这仅在实体被检测时才会显现?有没有办法改变删除级联顺序?
I have a complex object graph with JPA connected entities. When I delete the parent the deletions cascade correctly to the children.
Then I instrument the parent class (as to not load eagerly one-to-one relationships) and upon deletion I get referential integrity violation exceptions. Looking at the queries hibernate issues upon flush, I can see that hibernate indeed tries to delete records in a wrong order, thus the db complains for referential integrity and an exception is thrown.
Why this only manifests when the entities are instrumented? Is there a way to alter the delete cascade order?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有你的问题的答案,但是......你为什么要搞乱“仪器”来使你的一对一关联延迟加载?我已经测试了以下类
Foo
与其FooDetail
之间的一对一关联:并且延迟加载正常工作。这是检索
Foo
实例时执行的语句:然后,在调用 getter 时,会获取
FooDetail
:并删除给定的
Foo
实例也工作正常,语句按照正确的顺序执行:下面是 Hibernate 为相应表生成的 DDL,供参考:
使用 Hibernate Annotations 3.4.0.GA 进行测试。
更新:如果这不是您要查找的内容,请使用一对多关联,一对一关联存在局限性(请澄清您的问题,读者可以不要猜测你没有写什么)。
I don't have an answer to your question but... why are you messing with "instrumentation" to make your one-to-one association lazy-loaded? I have tested the following for a one-to-one association between a class
Foo
and itsFooDetail
:And lazy-loading just works. This is the statement performed when retrieving a
Foo
instance:And, later, when calling the getter, the
FooDetail
is fetched:And deleting a given
Foo
instance also works fine, the statements are executed in the right order:Below, the DDL generated by Hibernate for the corresponding tables for reference:
Tested with Hibernate Annotations 3.4.0.GA.
Update: If this is not what you're looking for, then use a one-to-many association, there are limitations with a one-to-one association (and please clarify your question, readers can't guess what you don't write).