Spring 和 Hibernate:如何声明与复合键的连接(使用注释)? [英] Spring and Hibernate : how to declare a join to composite key (using annotations)?

查看:25
本文介绍了Spring 和 Hibernate:如何声明与复合键的连接(使用注释)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to join the "last version" number of my Project to the table Project_version. How do I declare it with annotations @JoinColumn, etc ?

我的表项目":

* Project_id
  Title
  ...
  LastVersion_id

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

My table "Project_version" (primary key = Project_id + Version_id) :

* Project_id
* Version_id
  DateCreat
  ...

我的 bean "Project" :我需要声明属性 "LastVersion_id" 才能加入 Project_version :

My bean "Project" : I need declare the attribute "LastVersion_id" to join 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;

推荐答案

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

OK, I finally found the answer :

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

Note that @MapsId is absolutely required, otherwise you got the error "Repeated column in mapping for entity"

这篇关于Spring 和 Hibernate:如何声明与复合键的连接(使用注释)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆