合并 JPA 实体返回旧值
我有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Dirk,我也遇到过类似的问题,并且解决方案(我可能没有正确利用 API)非常密集。 Eclipselink 维护对象的缓存,如果它们没有更新(合并/持久),数据库通常会反映更改,但级联对象不会更新(特别是父对象)。
(我已将 A 声明为连接多个 B 的记录)
实体:
在上面的情况下,解决方法是:
您在这里所做的是将合并从顶部沿着链物理级联。维护起来很烦人,但确实解决了问题。或者,您可以通过检查它是否分离并刷新/更换来处理它,但我发现这样做不太理想且令人讨厌。
如果有人对正确的设置有更好的答案,我会很高兴听到。现在我已经对我的关系实体采用了这种方法,维护起来肯定很烦人。
祝你好运,我很想听到更好的解决方案。
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:
In the case above a workaround is:
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.