我有两个Entity像下面...
@Entity
@Table(name = "USER")
public class User {
@Id
private Long id;
private String name;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "groupMemberList")
@Fetch(FetchMode.SELECT)
private List<Group> groupList = new ArrayList<>();
// Getters - Setters
}
@Entity
@Table(name = "GROUP")
public class Group {
@Id
private Long id;
private String name;
@ManyToMany(fetch = FetchType.LAZY/*, mappedBy = "groupList"*/)
@Fetch(FetchMode.SELECT)
@JoinTable(name = "SEC_GROUP_VS_MEMBER", joinColumns = @JoinColumn(name = "GROUP_ID"),
inverseJoinColumns = @JoinColumn(name = "MEMBER_ID"))
private List<User> groupMemberList;
// Getters - Setters
}
我想User有时和Group有时使用以下方法更新...
方法#1
public boolean updateGroup(Long groupId, List<Staff> groupMemberList) {
Group group = hibernateTemplate.get(Group.class, groupId);
group.setGroupMemberList(groupMemberList);
hibernateTemplate.merge(group); // Group updated with the users
return true;
}
方法#2
public boolean updateUser(Long userId, List<Group> groupList) {
User user = hibernateTemplate.get(User.class, userId);
user.setGroupList(groupList);
hibernateTemplate.merge(user); // User not updated with the groups
return true;
}
第一种方法效果很好,但第二种方法不行。但是当我join table从第二个方法移动Group.class到User.class第二个方法时工作正常,而不是第一个。
小怪兽爱吃肉
相关分类