猿问

更新 JPA 或 Hibernate 中的多对多关系

我有两个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第二个方法时工作正常,而不是第一个。


倚天杖
浏览 215回答 1
1回答

小怪兽爱吃肉

问题是一个Owning Entity问题。假设它Staff是 的子类User,您的问题是关系的只有一方是拥有实体。这mappedBy = "groupMemberList"使Group实体成为拥有实体,因此只保留对该实体的更改。这意味着,你必须更新groupMemberList在Group这两种情况下的实体。如果您有一个组列表,User那么您必须遍历组列表并将其添加User到其中。该groupList在User仅用于检索。给定User和GroupMember实体:@Entitypublic class User {&nbsp; &nbsp; @Id @GeneratedValue&nbsp; &nbsp; private Long id;&nbsp; &nbsp; @ManyToMany(mappedBy = "groupMemberList")&nbsp; &nbsp; private List<GroupMember> groupList;@Entitypublic class GroupMember {&nbsp; &nbsp; @Id @GeneratedValue&nbsp; &nbsp; private Long id;&nbsp; &nbsp; @ManyToMany&nbsp; &nbsp; private List<User> groupMemberList;然后:// create starting user and membergroup&nbsp; &nbsp;&nbsp;tx.begin();User user = new User();em.persist(user);GroupMember group = new GroupMember();em.persist(group);tx.commit();em.clear();// update users for groupId 2System.out.println("update users for groupId 2");tx.begin();List<User> users = new ArrayList<>();users.add(user);group.setGroupMemberList(users);em.merge(group);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;tx.commit();em.clear();// update groups for userId 1 -- doesn't work, not owner of relationshipSystem.out.println("update groups for userId 1 -- doesn't work, not owner of relationship");tx.begin();List<GroupMember> groups = new ArrayList<>();groups.add(group);user.setGroupList(groups);em.merge(user);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;tx.commit();em.clear();// update groups for userId 1 -- worksSystem.out.println("update groups for userId 1 -- works");tx.begin();for ( GroupMember groupMember: groups) {&nbsp; &nbsp; groupMember.getGroupMemberList().add(user);&nbsp; &nbsp; em.merge(groupMember);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}tx.commit();em.clear();提供以下 SQL 输出:Hibernate: insert into User (id) values (?)Hibernate: insert into GroupMember (id) values (?)update users for groupId 2Hibernate: select groupmembe0_.id as id1_0_0_ from GroupMember groupmembe0_ where groupmembe0_.id=?Hibernate: select groupmembe0_.groupList_id as groupLis1_1_0_, groupmembe0_.groupMemberList_id as groupMem2_1_0_, user1_.id as id1_4_1_ from GroupMember_User groupmembe0_ inner join User user1_ on groupmembe0_.groupMemberList_id=user1_.id where groupmembe0_.groupList_id=?Hibernate: select user0_.id as id1_4_0_ from User user0_ where user0_.id=?Hibernate: insert into GroupMember_User (groupList_id, groupMemberList_id) values (?, ?)update groups for userId 1 -- doesn't work, not owner of relationshipHibernate: select user0_.id as id1_4_0_ from User user0_ where user0_.id=?Hibernate: select groupmembe0_.id as id1_0_0_ from GroupMember groupmembe0_ where groupmembe0_.id=?update groups for userId 1 -- worksHibernate: select groupmembe0_.id as id1_0_0_ from GroupMember groupmembe0_ where groupmembe0_.id=?Hibernate: select groupmembe0_.groupList_id as groupLis1_1_0_, groupmembe0_.groupMemberList_id as groupMem2_1_0_, user1_.id as id1_4_1_ from GroupMember_User groupmembe0_ inner join User user1_ on groupmembe0_.groupMemberList_id=user1_.id where groupmembe0_.groupList_id=?Hibernate: delete from GroupMember_User where groupList_id=?Hibernate: insert into GroupMember_User (groupList_id, groupMemberList_id) values (?, ?)Hibernate: insert into GroupMember_User (groupList_id, groupMemberList_id) values (?, ?)
随时随地看视频慕课网APP

相关分类

Java
我要回答