Hibernate、检测和删除级联顺序

发布于 2024-08-22 11:59:30 字数 193 浏览 10 评论 0原文

我有一个带有 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 技术交流群。

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

发布评论

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

评论(1

酷遇一生 2024-08-29 11:59:30

我没有你的问题的答案,但是......你为什么要搞乱“仪器”来使你的一对一关联延迟加载?我已经测试了以下类 Foo 与其 FooDetail 之间的一对一关联:

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
public FooDetail getFooDetail() {
    return detail;
}

并且延迟加载正常工作。这是检索 Foo 实例时执行的语句:

Hibernate: select foo0_.id as id45_0_, foo0_.detail_id as detail3_45_0_, foo0_.shortName as shortName45_0_ from Foo foo0_ where foo0_.id=?

然后,在调用 getter 时,会获取 FooDetail

Hibernate: select foodetail0_.id as id46_0_, foodetail0_.fullName as fullName46_0_ from FooDetail foodetail0_ where foodetail0_.id=?

并删除给定的 Foo 实例也工作正常,语句按照正确的顺序执行:

Hibernate: delete from Foo where id=?
Hibernate: delete from FooDetail where id=?

下面是 Hibernate 为相应表生成的 DDL,供参考:

create table Foo (id bigint not null, shortName varchar(255), detail_id bigint, primary key (id))
create table FooDetail (id bigint not null, fullName varchar(255), primary key (id))
alter table Foo add constraint FK212C3F68B31178 foreign key (detail_id) references FooDetail

使用 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 its FooDetail :

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
public FooDetail getFooDetail() {
    return detail;
}

And lazy-loading just works. This is the statement performed when retrieving a Foo instance:

Hibernate: select foo0_.id as id45_0_, foo0_.detail_id as detail3_45_0_, foo0_.shortName as shortName45_0_ from Foo foo0_ where foo0_.id=?

And, later, when calling the getter, the FooDetail is fetched:

Hibernate: select foodetail0_.id as id46_0_, foodetail0_.fullName as fullName46_0_ from FooDetail foodetail0_ where foodetail0_.id=?

And deleting a given Foo instance also works fine, the statements are executed in the right order:

Hibernate: delete from Foo where id=?
Hibernate: delete from FooDetail where id=?

Below, the DDL generated by Hibernate for the corresponding tables for reference:

create table Foo (id bigint not null, shortName varchar(255), detail_id bigint, primary key (id))
create table FooDetail (id bigint not null, fullName varchar(255), primary key (id))
alter table Foo add constraint FK212C3F68B31178 foreign key (detail_id) references FooDetail

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).

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