2

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

我的表“项目”:

* Project_id
  Title
  ...
  LastVersion_id

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

* Project_id
* Version_id
  DateCreat
  ...

我的 bean “ Project ”:我需要声明属性“ 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;
4

1 回答 1

4

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

@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是绝对必需的,否则您会收到错误“实体映射中的重复列”

于 2013-11-13T20:02:55.373 回答