带有注释的关联@ElementCollection
对以下映射类型有效:
Map<Basic,Basic>
Map<Basic,Embeddable>
Map<Embeddable,Basic>
Map<Embeddable,Embeddable>
Map<Entity,Basic>
Map<Entity,Embeddable>
@OneToMany
用/注释的关联@ManyToMany
对以下映射类型有效:
Map<Basic,Entity>
(这是你的情况)
Map<Embeddable,Entity>
Map<Entity,Entity>
根据上述规则,实体可能如下所示:
@Entity
public class ImageSet {
...
@OneToMany(mappedBy="container")
@MapKey //map key is the primary key
private Map<Integer, Image> images = new LinkedHashMap<>();
}
@Entity
public class Image {
...
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
private ImageSet container;
}
请注意,和之间的双向关联ImageSet.images
是Image.container
可选的,但删除它会在数据库中创建一个额外的表。