@Entity
public class Group
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key id;
}
@Entity
public class User
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key id;
@ManyToOne(fetch = FetchType.LAZY)
private Group group;
}
After the following code :
EntityManager entityManager = EntityManagerFactoryHolder.getEntityManagerFactory().createEntityManager();
Group group = new Group();
entityManager.persist(group);
User user = new User();
user.setGroup(group);
entityManager.persist(user);
entityManager.close();
I get the following error
Detected attempt to establish User(28) as the parent of Group(27) but
the entity identified by Group(27) has already been persisted without
a parent. A parent cannot be established or changed once an object
has been persisted.
org.datanucleus.store.appengine.DatastoreRelationFieldManager$ChildWithoutParentException:
Detected attempt to establish User(28) as the parent of Group(27) but
the entity identified by Group(27) has already been persisted without
a parent. A parent cannot be established or changed once an object
has been persisted.
Seems that this only works with back association. Added @OneToMany set to Group object and it works now.