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 ManyToMany relationship to User (a message can be sent to multiple users).
I am joining the t开发者_高级运维wo entities using @JoinTable, however, I would like to add a "status" column to the join table to tell if the message is new, read, etc. I was thinking of putting the column in the Message entity, however, I'm thinking this is probably not possible.
I've seen a lot of answers to this question saying to use an intermediary entity, but I would like to avoid that if possible.
Does anyone have any possible solution to my problem?
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>();
...
}
精彩评论