【问题标题】:Spring and Hibernate : how to declare a join to composite key (using annotations)?Spring 和 Hibernate:如何声明与复合键的连接(使用注释)?
【发布时间】:2013-11-25 01:01:12
【问题描述】:

我需要将我的项目的“最新版本”编号加入表 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;

【问题讨论】:

  • 你说得对,这与 Spring 无关。但 Hibernate 更常与 Spring 一起使用。

标签: hibernate join annotations


【解决方案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 是绝对必需的,否则会出现错误“实体映射中的重复列”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多