【发布时间】:2020-05-28 04:13:49
【问题描述】:
我有两个实体/表
为简洁起见删除了一些列
组:
@Entity
@Table(name = "groups", indexes = {@Index(name = "groups_index", columnList = "id", unique = true)})
public class Group implements Serializable {
@OneToMany(targetEntity = UserInGroup.class, mappedBy = "group")
private Set<UserInGroup> userInGroups = new HashSet<>();
}
UserInGroup:
@Entity
@Table(name = "user_in_group", indexes = {@Index(name = "user_in_group_index", columnList = "group_id,user_id", unique = true)})
public class UserInGroup implements Serializable {
@ManyToOne
@JoinColumn(name = "group_id", referencedColumnName = "id")
private Group group;
}
现在我在 UserInGroup 表中创建了一个条目,但 group 实体并没有使用 UserInGroup 中的新条目进行更新强>表。
群组未更新
在群组中创建的条目
编辑:
基于这个answer我添加了以下内容
UserInGroup userInGroup = new UserInGroup().setGroup(group).setUser(creatorUser).setGroupRoleEnum(GroupRoleEnum.ADMIN);
group.addUserInGroup(userInGroup);
groupRepository.save(group);
public void addUserInGroup(UserInGroup userInGroup) {
if (userInGroups == null) {
userInGroups = new ArrayList<>();
}
userInGroups.add(userInGroup);
userInGroup.setGroup(this);
}
但表 UserInGroup 仍然没有创建任何条目。
【问题讨论】: