WebNoob
WebNoob

Reputation: 259

ManyToMany - JPA - How to get data from mapping generated table?

I have 2 entities, Group and Members. They are mapped using @ManyToMany relationship. I am using Spring Data JPA for the mapping and DB operations.

GroupEntity

@ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(
            name="Group_Members",
            joinColumns= @JoinColumn(name="id"),
            inverseJoinColumns= @JoinColumn(name="memberId")            
    )
    Set<MemberEntity> members = new HashSet<>();

MemberEntity

@ManyToMany(cascade=CascadeType.ALL, mappedBy="members")
    Set<GroupEntity> groups = new HashSet<>();

I have successfully implemented Create, Read and Delete operations. The issue I have is with Update.

I have 3 tables as expected, Group - contains Group primary key and other details, Members- contains members primary key and members details that are created while creating the Group, and Group_Members table - which is the join table that contains Group Id and Member Ids belonging to individual groups. So far so good. Works well.

Now for Update, when I receive the request, I don't simply need to save that Group and hope that Cascade Merge will take care of it. What I need instead is to fetch the list of members for that Group first and then do a check, in case if a member is removed, is it allowed to be removed or not.

There are certain members which cant be removed from the Group for some business reasons. So if I happen to come across a Update request trying to remove a member which should not be removed, I need to send back a proper message.

For this, I need to fetch the list of all members for that Group. Now I am not sure how to do this. Because I don't have any handle on that 3rd table which stores the Group and its member mapping. All I have are 2 CrudRepository instances for Group and Member which I use to perform operations related to them.

@Repository
public interface GroupRepository extends CrudRepository<GroupEntity, Integer> {

@Repository
public interface MemberRepository extends CrudRepository<MemberEntity, Integer>{

The help I need here is in finding out how to fetch the list of all members for any particular group in case of ManyToMany mapping using Spring Data JPA?

Please let me know if anyone of you can suggest something to help me out.

Upvotes: 0

Views: 227

Answers (2)

Yuriy Tsarkov
Yuriy Tsarkov

Reputation: 2568

Your question looks a little bit weird - you already have a Set but not a List of Members in a GroupEntity, so what is a problem? In a service class e.g. GroupService add a method like this:

public void removeMember(Integer groupId, Integer memberId) {
    GroupEntity group = groupManager.getById(groupId);

    Set<MemberEntity> members = group.getMembers();

    MemberEntity member = members.stream().filter(item -> Objects.equals(memberId, item.getId())).findFirst().orElse(null);

    if (Objects.nonNull(member)) {
        if (/** here is you condition of making a decision of deletion **/) {
            members.remove(member);
            groupManager.save(group);
        } else {
            // here is an action in case of remove canceling        
        }
    }
}

Upvotes: 1

Alex Faster
Alex Faster

Reputation: 209

  1. Fetch all members that gonna be removed
  2. Verify that it is possible to remove them (need clarification from your side here, algorithm, etc)
  3. If everybody is applicable to remove - remove them, otherwise throw an exception

Upvotes: 0

Related Questions