我有三个@PC 课程:
@PersistenceCapable
class A {
@PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
private String id;
@Persistent @Embedded
private B b;
public void setB(B b){
this.b=b;
}
}
@PersistenceCapable @EmbeddedOnly
class B {
@Persistent
private String someInfo;
@Persistent
private C c;
public void setC(C c){
this.c=c;
}
}
@PersistenceCapable
class C {
@PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
private String id;
@Persistent
private String value;
public void setValue(String value){
this.value=value;
}
}
我想实现 B 在持有对 C 的引用的同时保持与 A 相同的实体,但 GAE 不允许我,我在提交时收到以下异常:
Detected attempt to establish A(1) as the parent of C(2) but the entity identified by C(2) has already been persisted without a parent. A parent cannot be established or changed once an object has been persisted.
在这段代码中:
A a = new A();
B b = new B();
C c = new C();
c.setValue("foo");
b.setC(c);
a.setB(b);
m.makePersistent(a);
另外:查看 DatastoreViewer 显示 C 已被持久化!但是A不见了。这可能会发生,因为我没有明确回滚在这种情况下不相关的异常事务,但表明 C 是在其父 A 之前编写的。
我错过了什么?发送
更新 2:
正如建议的那样,我已明确启用事务:
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(a);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
与执行 .makePersistent() w/o 显式事务时抛出的异常相同。然后我在 JDO 配置中设置禁用全局交叉 tx 选项:
<property name="datanucleus.appengine.datastoreEnableXGTransactions" value="false"/>
现在得到一个可能提示的不同异常:
cross-group transaction need to be explicitly specified, see
TransactionOptions.Builder.withXGfound both Element {
type: "A"
id: 1
}
and Element {
type: "C"
id: 2
}