【问题标题】:Google App Engine JPA getting com.google.appengine.datanucleus.EntityUtils$ChildWithoutParentExceptionGoogle App Engine JPA 获取 com.google.appengine.datanucleus.EntityUtils$ChildWithoutParentException
【发布时间】:2013-04-20 20:43:16
【问题描述】:

更新:我发现我的问题是我自己使用 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)

知道为什么会这样吗? 附: HRD 已启用。

【问题讨论】:

    标签: google-app-engine jpa google-cloud-datastore datanucleus


    【解决方案1】:

    因此,您在没有父实体的情况下保留了 FbUser,然后尝试在以后更改它,而 GAE 数据存储不允许这样做(正如消息中所说的非常清楚)。您没有提供持久性代码,因此除了猜测之外没有任何评论。

    解决方案:正确保留它(先是父,然后是子),或者将它们保留为Unowned

    【讨论】:

    • 我已经坚持了父母。当使用应用程序引擎自动生成密钥时,我能够坚持整个事情。但是当涉及到我将密钥分配为字符串然后使用 Keyfactory.createKey 方法将其转换为密钥时,它会引发上述错误。所以我只是保持原样并继续使用自动密钥生成,尽管根据我们已经拥有的代码,这对我来说可能不是最佳选择。
    猜你喜欢
    • 2011-06-19
    • 1970-01-01
    • 2015-11-06
    • 2013-05-07
    • 2015-07-25
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 2016-09-26
    相关资源
    最近更新 更多