0
votes

Update: I found out the problem in my case is that I am generating the FbUser primary key by myself using keyfactory.createKey() method. If I change it to auto generate it works fine. But the problem is I don't want to because my data is in String format for the key. So I need to change the type from String to Key manually and then persist it.

I am using Google App Engine JPA and trying to have a oneToMany relationship amongst my entities.

@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;
}

And here FbUser as the child :

@Entity
public class FbUser {

@Id
private Key id;
private String name;
@ManyToOne(fetch=FetchType.LAZY)
private DummyParent dummyP;
}

So after that I instantiate the parent class set its id and set the users. But I get the following exception:

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)

Any idea why this is happening? P.s. HRD is already enabled.

1

1 Answers

2
votes

So you persisted FbUser without a parent entity and then try to change it at a later date, and GAE Datastore doesn't allow that (as the message says pretty clearly). You present no persistence code so no comment is possible other than guesswork.

Solution : persist it correctly (parent first, then child), or persist them as Unowned.