我们很难在 Google App Engine 项目中保存数据,我们有“客户”、“预订”和“房间”类。
我们的目标是映射它们之间的关系,从 Customer 到 Reservation 的一对多关系和从 Room 到同一个 Reservation 的一对多关系。
我们得到的例外是:
no.hib.mod250.asm2.model.Reservation.id 的元数据错误:不能有 java.lang.Long 主键并且是子对象(拥有字段是 no.hib.mod250.asm2.model.Customer .res)。
我们的代码如下:
客户.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;
(...)
}
房间.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;
(...)
}
预订.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;
(...)
}