0

我正在尝试对此进行注释:

public class                        Keyword {
    @Id @GeneratedValue
    private Integer                 id;
    //Missing annotation
    private Map<Keyword, Integer>   keywordRelated;
}

我找到了这个例子,但他们没有提供关系模型,而且它不是完全相同的模型。我不知道他们的桌子是怎样的。

4

1 回答 1

1

以下仅说明如何映射具有实体作为键的 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;
于 2013-02-03T19:08:28.727 回答