我无法通过地图设置一对多的Hibernate参考。我有3个表:UserTable
,MessageTable
和RecipientTable
。在这里,收件人表包含有关消息的信息-哪个用户已接收/阅读消息。这意味着每封邮件将具有多个关联的收件人。收件人就像消息的“用户角度”。我希望能够通过该特定邮件的RecipientTable
从MessageTable
获取receiverId
。我尝试按照以下步骤进行操作。
MessageTable.java
@Entity
@Table(name = "MESSAGE")
public class MessageTable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "MESSAGE_ID", nullable = false)
private Long messageId;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "SENDER_ID", nullable = false)
private UserTable sender;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "RECEIVER_ID", nullable = false)
private UserTable receiver;
@OneToMany(mappedBy = "message", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private Map<Long, RecipientTable> recipients = new HashMap<>();
RecipientTable.java
@Entity
@Table(name = "RECIPIENT")
public class RecipientTable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "RECIPIENT_ID", nullable = false)
private Long recipientId;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "SENDER_ID", nullable = false)
private UserTable sender;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "RECEIVER_ID", nullable = false)
private UserTable receiver;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "MESSAGE_ID", nullable = false)
private MessageTable message;
@Enumerated(EnumType.STRING)
@Column(name = "SEENSTATUS", nullable = false)
private SeenStatus seenStatus;
@Enumerated(EnumType.STRING)
@Column(name = "RECEPTIONSTATUS", nullable = false)
private ReceptionStatus receptionStatus;
所以问题出在这一行:
@OneToMany(mappedBy = "message", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private Map<Long, RecipientTable> recipients = new HashMap<>();
我尝试了各种操作,例如添加@MapKey(name = "receiver.userId")
,但都无济于事。实现该目标的正确方法是什么?