Spring和Hibernate:如何声明连接到复合键(使用注释)?

时间:2013-11-12 18:05:28

标签: hibernate join annotations

我需要将项目的“最后版本”号加入到Project_version表中。 如何用@JoinColumn等注释声明它?

我的表格“项目”:

* Project_id
  Title
  ...
  LastVersion_id

我的表格“ Project_version ”(主键= Project_id + Version_id ):

* Project_id
* Version_id
  DateCreat
  ...

我的bean“项目”:我需要声明属性“ LastVersion_id ”才能加入Project_version:

@Id
private Integer Project_id;
private String Title
...
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="LastVersion_id")
                    // How to declare the join to (Project_id + Version_id) ?
private Project_version pv;

1 个答案:

答案 0 :(得分:1)

好的,我终于找到了答案:

@Id
private Integer Project_id;
private String Title;
// ...
@MapsId("Project_id")
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumns({
    @JoinColumn(name="Project_id", referencedColumnName="Project_id"),
    @JoinColumn(name="LastVersion_id", referencedColumnName="Version_id")
})
private Project_version pv;

请注意,@MapsId是绝对必需的,否则您会收到错误“实体映射中的重复列”