I have three entities User, Company, and Address with declarations like so:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class User implements Serializable {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent(mappedBy="creator")
@Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="title asc"))
private Collection<Company> companies;
@Persistent
private Address address;
. . .
}
public class Company implements Serializable {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
@Extension(vendorName="datanucleus", key="gae.parent-pk", value="true")
private Key creatorKey;
@Persistent
private User creator;
@Persistent
private Address address;
. . .
}
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Address implements Serializable{
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
. . .
}
After creating a user I then persist two companies owned by a user after setting their addresses and other information like so:
company.setCreatorKey(currentUser.getKey());
pm.makePersistent(company);
The problem arises when I retrieve a company and from that object reference the creator. Apparently JDO thinks that the addresses of the two companies belong to the user as opposed to the companies and the following error is logged:
address is mapped as a 1 to 1 relationship but there is more than one enity of kind Address that is a child of User([email protected])
The key of the user is
User([email protected])
the key of the company is
User([email protected])\Company(1)
and the key of the two addresses are
User([email protected])\Company(1)\Address(1)
User([email protected])\Company(1)\Address(2)
I'm pretty new to hierarchical databases so I was wondering why the two addresses are persisted/interpreted as children of user instead of descendants of user and children of company. What is the correct hierarchy for a situation like this? Thanks.