开发者

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

开发者 https://www.devze.com 2022-12-11 21:34 出处:网络
We are having a hard time persisting data in our Google App Engine project, we have the classes \"Customer\", \"Reservation\", and \"Room\".

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;  
    (...)  
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消