“ Java Persistence 2.0, Final Release ” 第 404 页有以下示例:
示例 3:从可嵌入类到另一个实体的一对一关联。
@Entity
public class Employee {
@Id int id;
@Embedded LocationDetails location;
...
}
@Embeddable
public class LocationDetails {
int officeNumber;
@OneToOne ParkingSpot parkingSpot;
...
}
@Entity
public class ParkingSpot {
@Id int id;
String garage;
@OneToOne(mappedBy="location.parkingSpot") Employee assignedTo;
...
}
我想在 Employee 中有多个 LocationDetails:
@Entity
public class Employee {
@Id int id;
@ElementCollection
@CollectionTable(name="EMP_LOCATION")
Map<String, LocationDetails> locations;
...
}
如何必须更改实体 ParkingSpot 以指向集合表 EMP_LOCATION 内的可嵌入 LocationDetails。
应该
@OneToOne(mappedBy="location.parkingSpot") Employee assignedTo;
替换为 @ElementCollection ?
谢谢你!