Stored data in GAE - Entity cannot have a long primary key and be a child

We have difficulty saving data in our Google App Engine project, we have the classes "Client", "Booking" and "Room".

Our goal is to correlate the relationship between them, with a one-to-many relationship from customer to reservation and a one-to-many relationship from room to same reservation.

The exception is:

Error in metadata for no.hib.mod250.asm2.model.Reservation.id: cannot have the primary key java.lang.Long and be a child (the owner 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; (...) } 
+4
source share
1 answer

As the message shows, you cannot use a long primary key if your object is a child of the object, which in this case is true. Instead, use a key or an encoded string as the primary key. See here for more details.

You should probably also read child objects and relationships .

+11
source

All Articles