我正在尝试对此进行注释:
public class Keyword {
@Id @GeneratedValue
private Integer id;
//Missing annotation
private Map<Keyword, Integer> keywordRelated;
}
我找到了这个example但他们不提供关系模型,而且它不是完全相同的模型。而且我不知道他们的 table 是怎样的。
最佳答案
下面仅讲述如何映射以实体为键的Map(根据情况可能有更好的解决方案,例如带有比率的中间对象)。
@ElementCollection是为映射此类集合而设计的。当值不是实体时不能使用@OneToMany。
默认遵循映射
@ElementCollection
private Map<Keyword, Integer> keywordRelated;
映射到数据库中的下表(假设关键字实体的表名称是关键字并且不受@Table-annotation影响):
Keyword_KEYWORDRELATED (
Keyword_ID (PK, FK to Keyword ID),
KEYWORDRELATED ,
keywordRelated_KEY (FK to Keyword ID)
)
如果数据库表和列的默认命名不够,可以按如下方式自定义:
@ElementCollection
@CollectionTable(name= "keyword_to_related_keyword")
@Column(name="ratio")
@MapKeyColumn(name="related_keyword_id")
@MapKeyJoinColumn(name="some_other_preferred_name")
public Map<Keyword, Integer> keywordRelated;
关于java - JPA注释映射与hibernate,以及最佳关系模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14673691/