Persisting data in GAE - Entity cannot have a Long primary key and be a child object

entity-relationship, google-app-engine, jakarta-ee, java, jdo

Solution

As the message suggests, you can't use a long as your primary key if your entity is a child entity, which is true in this case. Instead, use a key or encoded string as your primary key - see here for details.

You should probably also read up on child objects and relationships.

Problem

We are having a hard time persisting data in our Google App Engine project, we have the classes "Customer", "Reservation", and "Room". Our goal is to map a relation between these, with a one-to-many relation from Customer to Reservation and a one-to-many relation from Room to the same Reservation. The exception we get is: Error in meta-data for no.hib.mod250.asm2.model.Reservation.id: Cannot have a java.lang.Long primary key and be a child object (owning field is no.hib.mod250.asm2.model.Customer.res). Our code is as follows: Customer.java ``` @PersistenceCapable(identityType=IdentityType.APPLICATION) public class Customer implements Serializable { @PrimaryKey @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY) private Long id; (...) //an customer has one or more reservations. @Persistent(mappedBy="customer") private List <Reservation> res; (...) } ``` Room.java ``` @PersistenceCapable(identityType=IdentityType.APPLICATION) public class Room implements Serializable { @PrimaryKey @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY) private Long id; (...) //a room has one or more reservations @Persistent(mappedBy="room") private List<Reservation> res; @Persistent private Hotel hotel; (...) } ``` Reservation.java ``` @PersistenceCapable(identityType=IdentityType.APPLICATION) public class Reservation implements Serializable { @PrimaryKey @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY) private Long id; (...) @Persistent private Room room; @Persistent private Customer customer; (...) } ```

Original source