在 JPA 2 w/Hibernate 中强制传递持久性顺序?
有没有办法强制 JPA 2 w/Hibernate 中对象的持久顺序?
假设我有三个类:Parent
、Child
和 Desk
。 Parent
通过 @OneToMany
拥有 Child
和 Desk
的集合;一个孩子
可以拥有一张桌子
。此外,Parent
在两个集合上定义了传递持久性,但 Child
没有在其 Desk
上定义传递持久性:
class Parent {
@OneToMany(cascade=CascadeType.ALL) Collection<Child> children;
@OneToMany(cascade=CascadeType.ALL) Collection<Desk> desks;
...
}
class Child {
@OneToOne(cascade={}) Desk desk;
@ManyToOne Parent parent;
}
class Desk {
@ManyToOne Parent parent;
}
理想情况下,我想创建同时拥有一个 Desk 和一个 Child 并持久化关系:
Parent parent = em.find(...);
Child child = new Child();
Desk desk = new Desk();
// add both desk and child to parent collections here
// set Parent attribute on both desk and child
如果我在事务中执行上述代码,Hibernate 会从 Parent
级联到其新的 Child
并尝试持久化新的子对象
。不幸的是,这会导致“对象引用未保存的瞬态实例”错误,因为从 Parent
到 Desk
的级联并未产生新的 Desk 对象尚未被持久化。
我知道我可以通过 EntityManagerlush() 操作 (em.flush()
) 解决问题 - 创建 Child
,创建 Desk
,将两者附加到 Parent
、em.flush()
,然后将 Desk
附加到 Child
,但我'我不太热衷于乱扔我的代码em.flush()
保存新持久对象的复杂图。是否有不同的方式向 JPA 2 或 Hibernate 发出信号,表明它应该始终首先保留新的 Desk
而不是新的 Child
?
Is there any way to force the persistence order of objects in JPA 2 w/Hibernate?
Say I have three classes: Parent
, Child
, and Desk
. Parent
owns collections of Child
and Desk
via @OneToMany
; a Child
can have one Desk
. Furthermore, Parent
defines transitive persistence on both collections, but Child
does not define transitive persistence on its Desk
:
class Parent {
@OneToMany(cascade=CascadeType.ALL) Collection<Child> children;
@OneToMany(cascade=CascadeType.ALL) Collection<Desk> desks;
...
}
class Child {
@OneToOne(cascade={}) Desk desk;
@ManyToOne Parent parent;
}
class Desk {
@ManyToOne Parent parent;
}
Ideally, I'd like to create a Desk and a Child at the same time and persist the relationships:
Parent parent = em.find(...);
Child child = new Child();
Desk desk = new Desk();
// add both desk and child to parent collections here
// set Parent attribute on both desk and child
If I execute the above code in a transaction, Hibernate cascades from Parent
to its new Child
and attempts to persist the new Child
object. Unfortunately, this results in an "object references an unsaved transient instance" error, because the cascade from Parent
to Desk
hasn't resulted in the new Desk
object being persisted yet.
I know I can fix the problem with an EntityManager flush() operation (em.flush()
) - create the Child
, create the Desk
, attach both to Parent
, em.flush()
, then attach the Desk
to Child
, but I'm not super-keen on littering my code with em.flush()
to save complex graphs of new persistent objects. Is there a different way to signal to JPA 2 or Hibernate that it should always persist the new Desk
first instead of the new Child
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看你的描述,我认为持久化系统首先尝试按以下顺序持久化:
然后在持久化 Desk 时失败,并且您认为它在路径 Parent.desks[i] (配置为 Cascade)中失败,但也许失败不是来自该路径。
Looking at your description, I think that the Persistence system tries to persist first in this order:
Then it fails when persisting Desk, and you think that it fails in the path Parent.desks[i] (which is configured as Cascade) but maybe the fail doesn't come from this path.