我的实体之间有一个复杂的层次结构。由于错误(见下文),我在许多地方放置了 CASCADE.ALL 或 CASCADE.PERSIST。从上到下,注释都可以。但从下到上,这不是我想要的。
它导致对象被重复保存。
我该如何解决这个问题?
PS 为什么我需要 C 和 F 之间的关系:因为这是我获取 F 对象的标准方式。(目前 %90 的用例)
** Java 代码 -- 创建 A-Tree **
public A convertToA(final QueryAType aType, final A parent) {
final A a = new A();
if (parent != null) {
a.setVater(parent);
}
final List<A> children = new ArrayList<A>();
for (final QueryAType childPart : aType.getUnterbauteil()) {
children.add(convertToA(childPart, a));
}
final List<B> bList = new ArrayList<B>();
for (final QueryBType bType : aType.getBType()) {
bList.add(convertToB(bType, a));
}
a.setBList(bList);
a.setKinder(children);
return a;
}
public B convertToB(final QueryBType bType, final A a) {
final B b = new B();
b.setA(a);
final List<C> cList = new ArrayList<C>();
for (final QueryCType cType : bType.getCType()) {
cList.add(convertToC(cType, b));
}
b.setCList(cList);
return b;
}
public C convertToC(final QueryCType cType, final B b) {
final C c = new C();
c.setB(b);
final List<D> dList = new ArrayList<D>();
for (final QueryDType dType : cType.getDType()) {
dList.add(convertToD(dType, c));
}
c.setDList(dList);
return c;
}
public D convertToD(final QueryDType dType, final C c) {
final D d = new D();
d.setProbe(c);
final List<E> eList = new ArrayList<E>();
for (final QueryEType eType : dType.getEType()) {
eList.add(convertToE(eType, d, c));
}
d.setEList(eList);
return d;
}
public E convertToE(final QueryEType eType, final D d, final C c) {
final E e = new E();
e.setD(d);
final List<F> fList = new ArrayList<F>();
for (final QueryFType fType : eType.getFType()) {
fList.add(convertToF(fType, e, c));
}
e.setFList(fList);
c.setFList(fList);
return e;
}
public F convertToF(final QueryFType fType, final E e, final C c) {
final F f = new F();
f.setC(c);
f.setE(e);
final List<G> gList = new ArrayList<G>();
for (final QueryGType gType : fType.getGType()) {
gList.add(convertToG(gType, f));
}
f.setGList(gList);
return f;
}
public G convertToG(final QueryGType gType, final F f) {
final G g = new G();
g.setMethode(f);
return g;
}
没有级联的错误:
org.apache.openjpa.persistence.InvalidStateException:在生命周期状态下遇到非托管对象“com.xxx.yyy.data.entity.E@18e0a217”,同时通过字段“com.xxx.yyy.data.entity.Fe”级联持久性“在冲洗期间。但是,该字段不允许级联持续存在。您不能刷新与非托管对象具有持久关联的非托管对象或图形。建议的操作:a) 将此字段的 cascade 属性设置为 CascadeType.PERSIST 或 CascadeType.ALL(JPA 注释)或“persist”或“all”(JPA orm.xml),b)全局启用 cascade-persist,c)手动在刷新之前保留相关的字段值。d) 如果引用属于另一个上下文,则通过设置 StoreContext.setAllowReferenceToSiblingContext() 允许对其进行引用。失败对象:com.xxx.yyy.data.entity。