【发布时间】:2018-09-22 18:44:39
【问题描述】:
我有四个实体 Message、User、Group、MessageRecipient。我在 Group 实体中添加了一个@ManyToMany 用户,jpa 自动创建额外的表 user_group,现在我希望这个用户组有自己的 id,所以我可以将它作为外键添加到 MessageRecipient Entity
目前,我有这个
@Entity
public class Group {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
@ManyToMany
private Set<User> users;
private boolean isActive;
private LocalDateTime dateCreated;
}
@Entity
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String subject;
@ManyToOne
@Column(name = "creatorId")
private User user;
private String messageBody;
private LocalDateTime dateCreated;
private Integer parentMessageId;
private LocalDateTime expiryDate;
private Integer isReminder;
private LocalDateTime nextRemindDate;
private Integer remindFrequencyId;
}
@Entity
public class MessageRecipient {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "recipient_Id")
private User user;
//this is where i should have FK of the user_group table
private Integer recipient_group_id;
@Column(name = "message_id")
private Message message;
private boolean isRead;
}
通过创建另一个实体来解决它
public class UserGroup {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private User user;
private Group group;
}
Group Entity 中的注解 @ManyToMany user 会发生什么情况以及由此导致的 user_group 的自动创建
【问题讨论】:
-
我的问题通常是 jpa 自动创建 user_group ,@K.Nicholas 那么如果我自己创建它并且仍然使用注释会发生什么,我的数据库中会有两个用户组实体吗?
-
@K.Nicholas 好的,我会检查一下,你能检查一下我的答案是否正确,我刚刚发布了一个解决方案
-
好的,我会阅读您发送的链接以了解问题,然后了解可嵌入复合键@K.Nicholas
标签: jpa