【发布时间】:2014-04-11 09:12:52
【问题描述】:
我想设置hibernate自动生成的外键约束名,所以不是fk_123213241341,而是fk_user。
我正在尝试使用新的 JPA 2.1 注释 @ForeignKey。但我可能遗漏了一些东西:
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;
}
【问题讨论】:
-
你想做什么?
User在对象UserPK中有id.firstname和id.lastname。那么许多trip可以有一个User。所以在旅行中你可以得到user.id.firstname和user.id.lastname。像这样? -
而您只想使用
@ForeignKey而不是@JoinColumns -
@ForeignKey已弃用,因为最新的休眠版本依赖于 JPA 2.1 -
是的,cz @javax.persistence.ForeignKey in JPA 2.1