在GAE中保留数据 - 实体不能具有Long主键并且是子对象

4 java google-app-engine entity-relationship jdo java-ee

我们很难在Google App Engine项目中保留数据,我们有"客户","预订"和"房间"等课程.

我们的目标是映射这些关系,从客户到预订的一对多关系以及从房间到同一预订的一对多关系.

我们得到的例外是:

no.hib.mod250.asm2.model.Reservation.id的元数据错误:不能有java.lang.Long主键并且是子对象(拥有字段是no.hib.mod250.asm2.model.Customer .RES).

我们的代码如下:

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;  
    (...)  
}  
Run Code Online (Sandbox Code Playgroud)

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;  
    (...)  
}   
Run Code Online (Sandbox Code Playgroud)

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;  
    (...)  
}
Run Code Online (Sandbox Code Playgroud)

Nic*_*son 11

如消息所示,如果您的实体是子实体,则不能使用long作为主键,在这种情况下也是如此.而是使用密钥或编码字符串作为主键 - 有关详细信息,请参阅此处.

您可能还应该阅读子对象和关系.