Hi! I have a problem mapping a composite foreign key within a composite primary key. That's pretty complicated and probably bad db design but I'm not the owner of the database and I won't be able to change anything at all.
Here is an example of my problem:
Code:
@Entity
@Table
public class Role implements java.io.Serializable {
@EmbeddedId
private RoleId id;
...
Code:
@Embeddable
public class RoleId implements java.io.Serializable {
@JoinColumns({
@JoinColumn(name = "firstname", referencedColumnName = "firstname", nullable = false),
@JoinColumn(name = "lastname", referencedColumnName = "lastname", nullable = false) })
private User user;
@Column(name = "groupid", nullable = false)
private int groupid;
...
Now I get the following MappingException:
Code:
Initial SessionFactory creation failed.org.hibernate.MappingException: Foreign key (FKF61F3A3F666B95E7:role [user])) must have same number of columns as the referenced primary key (user [firstname, lastname])
When using the @AttributeOverrides annotation I can only specify one referencing column name like "firstname" (instead of both, "firstname" and "lastname":
Code:
@Entity
@Table
public class Role implements java.io.Serializable {
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "user", column = @Column(name = "firstname", nullable = false, length = 14)),
@AttributeOverride(name = "groupid", column = @Column(name = "groupid", nullable = false)) })
private RoleId id;
...
Therefore I get the following exception (which of course is correct):
Code:
Initial SessionFactory creation failed.org.hibernate.MappingException: Foreign key (FKF61F3A3F5D0449F4:role [firstname])) must have same number of columns as the referenced primary key ((user [firstname, lastname])
Of course, nobody would use "firstname" and "lastname" as primary key, but it's just an example of my problem.