JPA 2.0 / Hibernate 和“orphanRemoval”:仅替换实体并不会删除旧实体

发布于 2024-12-06 01:48:37 字数 1402 浏览 2 评论 0原文

我对 JPA 2.0、Hibernate 和“orphanRemoval”有疑问。

首先我的设置:

  • Spring 3.0.5.RELEASE
  • SprnigData JPA 1.0.1.RELEASE
  • Hibernate 3.5.2-Final
  • DBMS:PostgreSQL 9.0

我有两个相当简单的实体类,“User”和“AvatarImage”,“User”有一个“ AvatarImage”,所以“User”和“AvatarImage”之间存在关系。

在“User”类中,该属性如下所示:

// class "User"
@OneToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY, orphanRemoval = true)
private AvatarImage    avatarImage;

这意味着,如果“avatarImage”属性设置为 null,则“User”和“AvatarImage”之间的引用将被删除,并且“orphanRemoval”机制将删除“avatarImage” “来自数据库(如果我错了,请纠正我)。

因此,当我更新某个用户的“avatarImage”时,我目前必须这样写:

user.setAvatarImage( null );  // First set it to null
userRepository.save( user );  // Now "orphanRemoval" will delete the old one

user.setAvatarImage( theNewAvatarImage );
userRepository.save( user );

因此,首先将“avatarImage”属性设置为 null,保存“user”,然后设置新的 AvatarImage“theNewAvatarImage”,再次保存用户。

这是目前对我有用的唯一方法 - “orphanRemoval”将删除旧的“avatarImage”,将其设置为“null”,然后保存用户。

但是,我认为这段代码也应该有效:

user.setAvatarImage( theNewAvatarImage );
userRepository.save( user );

所以我省略将“avatarImage”设置为“null”,而只是设置“theNewAvatarImage”,替换旧的“avatarImage”。但这不起作用,旧的 AvatarImage 在事务提交时不会从数据库中删除。

有谁知道,为什么第二个代码(只是替换 AvatarImage 而不之前将其设置为“null”)不起作用?

我真的很感谢您能提供的任何帮助,

非常感谢!

I have question regarding JPA 2.0, Hibernate and "orphanRemoval".

First my setup:

  • Spring 3.0.5.RELEASE
  • SprnigData JPA 1.0.1.RELEASE
  • Hibernate 3.5.2-Final
  • DBMS: PostgreSQL 9.0

I have two rather simple entity classes, "User" and "AvatarImage", A "User" has an "AvatarImage", and so between "User" and "AvatarImage" there is the relationship.

In the class "User", the property looks like this:

// class "User"
@OneToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY, orphanRemoval = true)
private AvatarImage    avatarImage;

So that means, if the "avatarImage" property gets set to null, the reference between "User" and "AvatarImage" is removed and "orphanRemoval" mechanism will delete the "avatarImage" from the database (please correct me if I'm wrong).

So when I update the "avatarImage" for a certain user, I currently have to write this:

user.setAvatarImage( null );  // First set it to null
userRepository.save( user );  // Now "orphanRemoval" will delete the old one

user.setAvatarImage( theNewAvatarImage );
userRepository.save( user );

So setting the "avatarImage" property first to null, saving the "user", and then set the new AvatarImage "theNewAvatarImage", again saving the user.

This is the only way it currently works for me - the "orphanRemoval" will delete the old "avatarImage" on setting it to "null" and then saving the user.

But, I would have thought that this code should also work:

user.setAvatarImage( theNewAvatarImage );
userRepository.save( user );

So I omit setting the "avatarImage" to "null" but just setting "theNewAvatarImage", replacing the old "avatarImage". But this does not work, the old AvatarImage does not get removed from the database upon transaction commit.

Does anyone know, why the second code (just replacing the AvatarImage without setting it to "null" before) does not work?

I really appreciate any help you can offer

Thanks a lot!

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

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

发布评论

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

评论(2

橘寄 2024-12-13 01:48:37

这与 Hibernate JIRA 票证 HHH-5559HHH-6484。总的来说,从今天开始,Hibernate 要求您在为关系提供新值之前将引用设置为 null 并刷新持久性上下文(请参阅 HHH-6484 中的测试用例);只有在这种情况下,Hibernate 才会发出 SQL DELETE 语句,为 orphanRemoval 提供损坏的实现(恕我直言)。

简而言之,您需要等待错误得到修复,或者编写代码来使引用无效并刷新持久性上下文,或者使用支持这种方式的 orphanRemoval 的 JPA 提供程序 (EclipseLink 2.3.1)。 0 是)。

This is related to Hibernate JIRA tickets HHH-5559 and HHH-6484. By and large, Hibernate, as of today, requires you to set the reference to null and flush the persistence context, before providing a new value to the relationship (see the test case in HHH-6484); it is only in such a case that Hibernate issues a SQL DELETE statement, providing a broken implementation (IMHO) for orphanRemoval.

In short, you'll need to wait for the bugs to be fixed, or write code to nullify references and flush the persistence context, or use a JPA provider that supports orphanRemoval in this manner (EclipseLink 2.3.0 does).

浮云落日 2024-12-13 01:48:37

至于 @OneToMany 关系,这与 Hibernate JIRA 票证 HHH-6709。请为这些投票,以便引起一些关注。

As of @OneToMany relationship, this is related to Hibernate JIRA ticket HHH-6709. Please vote for these so it get some attention.

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