我有4个与OneToMany关联映射的实体。因此实体A (OneToMany ) -> B (OneToMany ) -> C (OneToMany )-> D
我想一次保存整个结构。因此,我为每个cascade = CascadeType.ALL
输入了OneToMany
。但是似乎JPA不想保存C和D的关联,我完成的只是B引用了A,而C和D的保存没有引用。
当我使用c.setB(b)和d.setC(c)之类的引用保存它时,会给我错误object references an unsaved transient instance - save the transient instance before flushing
那么有可能用引用保存所有4个实体,还是我应该一步一步地用代码来做到这一点?
A:
@OneToMany(mappedBy = "a", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<B> b;
B:
@OneToMany(mappedBy = "b", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<C> c;
@JoinColumn(name = "aId")
@ManyToOne(fetch = FetchType.LAZY)
private A a;
C:
@OneToMany(mappedBy = "c", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<D> d;
@JoinColumn(name = "bId")
@ManyToOne(fetch = FetchType.LAZY)
private B b;
D:
@JoinColumn(name = "cId")
@ManyToOne(fetch = FetchType.LAZY)
private C c;
答案 0 :(得分:0)
我在C实体中添加了不存在的引用是我的错误。因此有可能)Hibernate完成了所有4级层叠!