I have two entities, Message and User. User has a ManyToMany relationship to Message (A user can have many messages) and Message (for now, to make it less complex) has a Man
JPA 2.0 (i.e. Hibernate 3.5 and above) introduced a support for modeling ternary relationships as Map
s. For example, you can do something like this (though I'm not sure what to do with the other side if you need a bidirectional relationship):
public enum MessageStatus { READ, UNREAD }
public class User {
...
@ElementCollection
@CollectionTable(name = "MessagesToUsers", joinColumns = @JoinColumn(name = "userId"))
@Column(name = "messageStatus")
@MapKeyJoinColumn(name = "messageId")
private Map<Message, MessageStatus> messages = new HashMap<Message, MessageStatus>();
...
}