Java 如何使用复合键在表上映射外键名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23008297/
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
How to map a foreign key name on table with composite key?
提问by membersound
I'd like to set the foreign key constraint name that is autogenerated by hibernate, so that is not named fk_123213241341
, but fk_user
.
我想设置由 hibernate 自动生成的外键约束名称,因此它不是 named fk_123213241341
,而是fk_user
.
I'm trying to use the new JPA 2.1 Annotation @ForeignKey
. But I'm missing probably something:
我正在尝试使用新的 JPA 2.1 Annotation @ForeignKey
。但我可能遗漏了一些东西:
org.hibernate.AnnotationException: A Foreign key refering User from Trip has the wrong number of column. should be 2
org.hibernate.AnnotationException: A Foreign key refering User from Trip has the wrong number of column. should be 2
@IdClass(UserPK.class)
class User {
@Id
String firstname;
@Id
String lastname;
//other fields omitted
}
class UserPK {
String firstname;
String lastname;
}
class Trip {
@ManyToOne
@JoinColumn(foreignKey = @ForeignKey(name = "FK_USER")
private User user;
}
采纳答案by Brian Vosburgh
You need to use the @JoinColumns
annotation (note the 's'
):
您需要使用@JoinColumns
注释(注意's'
):
class Trip {
@ManyToOne
@JoinColumns(foreignKey = @ForeignKey(name = "FK_USER")
private User user;
}