Supposing I have a many-to-many relationship with an attribute. I want to substitute this with two one-to-many relationships. If I configure the keys, I get a strange warning, that the primary key has an invalid number of attributes.
Here is what I want to have:
User <1:N> Notification <N:1> Thread
You should note, that I need notification to have a composite key constisting of user_id
and thread_id
.
That is what I've tried:
@Entity
public class Notification implements Serializable {
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "user_id", column = @Column(nullable = false)),
@AttributeOverride(name = "thread_id", column = @Column(nullable =false))
})
private NotificationPK id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id", insertable = false, updatable = false)
private User user;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "thread_id", insertable = false, updatable = false)
private Thread thread;
}
On the Thread class it looks like this:
@OneToMany(fetch=FetchType.LAZY)
@JoinTable(
name="notifications",
joinColumns=@JoinColumn(name="thread_id")
)
private Set<Notification> notifications = new HashSet<Notification>();
Using NotificationPK
as follows:
@Embeddable
public class NotificationPK implements Serializable {
Long user_id;
Long thread_id;
}
However, as soon as I startup, I get the following exception:
Caused by: org.hibernate.MappingException:
Foreign key (FK4BD694E87364C017:notifications
[notifications_thread_id,notifications_user_id]))
must have same number of columns as the referenced
primary key (notifications
[thread_id,notifications_thread_id,notifications_user_id])
What am I doing wrong? Why does he expects this to be part of the primary key?