Adding additional Property to hibernate JoinTable

前端 未结 1 1377
执笔经年
执笔经年 2021-01-01 06:57

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

相关标签:
1条回答
  • 2021-01-01 07:21

    JPA 2.0 (i.e. Hibernate 3.5 and above) introduced a support for modeling ternary relationships as Maps. 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>();
    
        ...
    }
    
    0 讨论(0)
提交回复
热议问题