|
Suppose we have the following entities:
@Entity
public class Friendship implements Serializable {
@Id
@ManyToOne
private Member requester;
@Id
@ManyToOne
private Member friend;
}
@Entity
public class Member implements Serializable {
@Id
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "username")
private Credential credential;
}
@Entity
public class Credential implements Serializable {
@Id
@Column(nullable = false, unique = true)
private String username;
}
When the above entities are added to the annotated classes list in the order
Credential, Friendship, Member
then the exception is thrown when the mappings are processed:
org.hibernate.MappingException: Foreign key (FK_8ynretl1yt1xe3guaheohrvpq:Friendship [])) must have same number of columns as the
referenced primary key (Member [username])
However, when the order is
Credential, Member, Friendship
everything works ok.
Also, if the Friendship mapping is changed by adding explicit referencedColumnName to the requester and friend fields, everything works ok regardless of the order in which the entities are added to the annotated classes list:
@Entity
public class Friendship implements Serializable {
@Id
@ManyToOne
@JoinColumn(referencedColumnName = "username")
private Member requester;
@Id
@ManyToOne
@JoinColumn(referencedColumnName = "username")
private Member friend;
}
|