合并 JPA 实体返回旧值

发布于 2024-12-06 07:08:27 字数 914 浏览 1 评论 0原文

我有 2 个 JPA 实体,它们之间具有双向关系。

@Entity
public class A {

     @ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE})
     B b;

     // ...
}

现在,

@Entity
public class B {

     @OneToMany(mappedBy="b",cascade={CascadeType.PERSIST, CascadeType.MERGE})
     Set<A> as = new HashSet<A>();

     // ...
}

我更新了分离的 A 的一些字段值,该值也与某些 B 有关系,反之亦然,并通过

public String save(A a) {
    A returnedA = em.merge(a);
}

returnedA 将其合并回来,现在有更新之前 A 的值。 我想这

  FINEST: Merge clone with references A@a7caa3be
  FINEST: Register the existing object B@cacf2dfb
  FINEST: Register the existing object A@a7caa3be
  FINEST: Register the existing object A@3f2584b8

表明 B 中引用的 As(仍然具有旧值)负责覆盖新值?

有谁有提示如何防止这种情况发生?

任何想法都将不胜感激!

提前致谢。

I have 2 JPA entities that have a bidirectional relationship between them.

@Entity
public class A {

     @ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE})
     B b;

     // ...
}

and

@Entity
public class B {

     @OneToMany(mappedBy="b",cascade={CascadeType.PERSIST, CascadeType.MERGE})
     Set<A> as = new HashSet<A>();

     // ...
}

Now I update some field values of a detached A which also has relationships to some Bs and vice versa and merge it back by

public String save(A a) {
    A returnedA = em.merge(a);
}

returnedA now has the values of A prior to updating them.
I suppose that

  FINEST: Merge clone with references A@a7caa3be
  FINEST: Register the existing object B@cacf2dfb
  FINEST: Register the existing object A@a7caa3be
  FINEST: Register the existing object A@3f2584b8

indicates that the referenced As in B (which still have the old values) are responsible for overwriting the new ones?

Does anyone have a hint how to prevent this to happen?

Any idea is greatly appreciated!

Thanks in advance.

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

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

发布评论

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

评论(1

夜吻♂芭芘 2024-12-13 07:08:27

Dirk,我也遇到过类似的问题,并且解决方案(我可能没有正确利用 API)非常密集。 Eclipselink 维护对象的缓存,如果它们没有更新(合并/持久),数据库通常会反映更改,但级联对象不会更新(特别是父对象)。

(我已将 A 声明为连接多个 B 的记录)
实体:

public class A
{
  @OneToMany(cascade = CascadeType.ALL)
  Collection b;
}

public class B
{
  @ManyToOne(cascade = {CascadeType.MERGE, CascadeType.REFRESH}) //I don't want to cascade a persist operation as that might make another A object)
  A a;
}

在上面的情况下,解决方法是:

public void saveB(B b) //"Child relationship"
{
  A a = b.getA();//do null checks as needed and get a reference to the parent
  a.getBs().add(b); //I've had the collection be null
  //Persistence here
  entityInstance.merge(a); // or persist this will cascade and use b
}

public void saveA(A a)
{
  //Persistence
  entityInstance.merge(a) // or persist
}

您在这里所做的是将合并从顶部沿着链物理级联。维护起来很烦人,但确实解决了问题。或者,您可以通过检查它是否分离并刷新/更换来处理它,但我发现这样做不太理想且令人讨厌。

如果有人对正确的设置有更好的答案,我会很高兴听到。现在我已经对我的关系实体采用了这种方法,维护起来肯定很烦人。

祝你好运,我很想听到更好的解决方案。

Dirk, I've had a similar problem and the solution (I might not be leveraging the API correctly) was intensive. Eclipselink maintains a cache of objects and if they are not updated (merged/persisted) often the database reflects the change but the cascading objects are not updated (particularly the parents).

(I've declared A as the record joining multiple B's)
Entities:

public class A
{
  @OneToMany(cascade = CascadeType.ALL)
  Collection b;
}

public class B
{
  @ManyToOne(cascade = {CascadeType.MERGE, CascadeType.REFRESH}) //I don't want to cascade a persist operation as that might make another A object)
  A a;
}

In the case above a workaround is:

public void saveB(B b) //"Child relationship"
{
  A a = b.getA();//do null checks as needed and get a reference to the parent
  a.getBs().add(b); //I've had the collection be null
  //Persistence here
  entityInstance.merge(a); // or persist this will cascade and use b
}

public void saveA(A a)
{
  //Persistence
  entityInstance.merge(a) // or persist
}

What you're doing here is physically cascading the merge down the chain from the top. It is irritating to maintain, but it does solve the problem. Alternatively you can deal with it by checking if it is detached and refreshing/replacing but I've found that to be less desirable and irritating to work with.

If someone has a better answer as to what the correct setup is I would be happy to hear it. Right now I've taken this approach for my relational entities and it is definitely irritating to maintain.

Best of luck with it, I'd love to hear a better solution.

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