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.
I'm trying to use the new JPA 2.1 Annotation @ForeignKey. But I'm missing probably something:
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;
}
You need to use the @JoinColumns annotation (note the 's'):
class Trip {
@ManyToOne
@JoinColumns(foreignKey = @ForeignKey(name = "FK_USER")
private User user;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With