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

时间:2009-11-17 12:53:39

标签: java google-app-engine java-ee entity-relationship jdo

我们很难在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;  
    (...)  
}  
  

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

1 个答案:

答案 0 :(得分:11)

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

您可能还应该阅读child objects and relationships