更新:我发现我的问题是我自己使用 keyfactory.createKey() 方法生成 FbUser 主键。如果我将其更改为自动生成它工作正常。但问题是我不想这样做,因为我的数据是密钥的字符串格式。所以我需要手动将类型从 String 更改为 Key 然后持久化。
我正在使用 Google App Engine JPA 并尝试在我的实体之间建立 oneToMany 关系。
@Entity
public class DummyParent{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
//@Unowned
@OneToMany(targetEntity=FbUser.class, mappedBy="dummyP", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
private ArrayList<FbUser> users;
}
在这里 FbUser 作为孩子:
@Entity
public class FbUser {
@Id
private Key id;
private String name;
@ManyToOne(fetch=FetchType.LAZY)
private DummyParent dummyP;
}
所以在那之后我实例化父类设置它的id并设置用户。但我得到以下异常:
Caused by: com.google.appengine.datanucleus.EntityUtils$ChildWithoutParentException: Detected attempt to establish DummyParent(no-id-yet) as the parent of FbUser("1322222") but the entity identified by FbUser("1322222") has already been persisted without a parent. A parent cannot be established or changed once an object has been persisted.
at com.google.appengine.datanucleus.EntityUtils.extractChildKey(EntityUtils.java:939)
at com.google.appengine.datanucleus.StoreFieldManager.getDatastoreObjectForCollection(StoreFieldManager.java:967)
at com.google.appengine.datanucleus.StoreFieldManager.storeFieldInEntity(StoreFieldManager.java:394)
知道为什么会这样吗?Ps HRD 已启用。