java 带有外键和主键公共列的 Hibernate 注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15301986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate annotation with common column for foreign and primary key
提问by Chris
I am having some issues modeling a foreign key the way I like it in hibernate 4.1.9
我在 hibernate 4.1.9 中以我喜欢的方式对外键进行建模时遇到了一些问题
I basically have the following code:
我基本上有以下代码:
@Entity
@XmlRootElement
@Table(name="User")
public class UserVO
{
private String email;
@Id
@Column(name="email", unique=true, nullable=false)
@XmlElement
public String getEmail()
{
return this.email;
}
}
and this:
还有这个:
@Entity
@XmlRootElement
@Table(name="Authentification")
public class AuthentificationVO
{
private String email;
private UserVO user;
@Id
@Column(name="email", unique=true, nullable=false)
@XmlElement
public String getEmail()
{
return this.email;
}
@OneToOne(cascade = CascadeType.ALL)
@ForeignKey(name="user_fk")
public UserVO getUser()
{
return this.user;
}
}
The point of it all, is to have a column named email in both tables, which should also serve as a primary key for both the User and Authentification tables. For some reason, when I run this, Hibernate insists on adding a column to the Authentification table called user_email, but I want it to use the already existing column named email.
这一切的重点是在两个表中都有一个名为 email 的列,它也应该作为 User 和 Authentification 表的主键。出于某种原因,当我运行它时,Hibernate 坚持向名为 user_email 的 Authentification 表添加一列,但我希望它使用名为 email 的现有列。
I tried looking around for a solution for this and I have tried using mappedBy annotation to get it right, along with JoinColumn, but nothing seems to do the trick.
我尝试四处寻找解决方案,并尝试使用mappedBy 注释和JoinColumn 使其正确,但似乎没有任何效果。
采纳答案by Chris
What actually did the trick in the end was adding the following annotation:
最后真正起作用的是添加以下注释:
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="email")
@MapsId
I removed the ForeignKey annotation, since it was disregarded when creating the table. The only effect is the naming of the constraint, which is now autogenerated by hibernate and I can live with that.
我删除了 ForeignKey 注释,因为在创建表时它被忽略了。唯一的影响是约束的命名,它现在由 hibernate 自动生成,我可以接受。
回答by benzonico
For a one to one you would need to specify @JoinColumn(name="user_fk")
for the mapping.
The @ForeignKey
annotation is just there to override the name of the foreign key generated by hibernate, not to tell to hibernate wich column to use (which is the role of the @JoinColumn
)
对于一对一,您需要指定@JoinColumn(name="user_fk")
映射。该@ForeignKey
注释只是为了覆盖由Hibernate生成的外键的名称,而不是告诉冬眠至极列使用(这是该角色@JoinColumn
)
[Edit after comments]
[评论后编辑]
Actually, as you are mapping on the primary key you should use the @MapsId
annotation to tell hibernate you are mapping to the Id of the OneToOne
associated entity.
Your mapping should then become :
实际上,当您在主键上进行映射时,您应该使用@MapsId
注释来告诉 hibernate 您正在映射到OneToOne
关联实体的 Id 。你的映射应该变成:
@OneToOne(CascadeType.ALL)
@ForeignKey(name="email")
@MapsId