-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Two collections with same cascades inconsistend behavior
PostPosted: Wed Jul 05, 2017 4:48 am 
Newbie

Joined: Sun Feb 24, 2008 5:12 pm
Posts: 9
I have three Entities: User, Role and Authority.
User has ManyToMany bi-directional mapping to Role and uni-directional ManyToMany mapping to Authority:
Code:
@Entity
public class User {
    @ManyToMany(cascade = {CascadeType.DETACH, CascadeType.REFRESH}, fetch = FetchType.EAGER)
    @JoinTable(name = "user_authority",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "authority_id"))
    private Set<Authority> authorities = new HashSet<>();

    @ManyToMany(mappedBy = "users", cascade = {CascadeType.DETACH, CascadeType.REFRESH}, fetch = FetchType.EAGER)
    private Set<Role> roles = new HashSet<>();

    //other stuff
}

@Entity
public class Role {
    @ManyToMany(cascade = {CascadeType.DETACH, CascadeType.REFRESH}, fetch = FetchType.EAGER)
    @JoinTable(name = "user_role",
            joinColumns = @JoinColumn(name = "role_id"),
            inverseJoinColumns = @JoinColumn(name = "user_id"))
    private Set<User> users = new HashSet<>();

    //other stuff
}

@Entity
public class Authority {
    //other stuff
}


and now when I run these tests:
Code:
@Test
public void shouldAddNewRoleToExistingUser() {
   //given
   final User user =  userRepository.findOne(userRepository.save(createRandomUser()).getId());
   final Role role = roleRepository.save(createRandomRole());

   //when
   User changedUser = userRepository.findOne(userService.changeRole(user, role).getId());

   //then
   assertThat(changedUser)
      .isNotNull();
   assertThat(changedUser.getRoles())
      .containsExactly(role);
}

@Test
public void shouldAddNewAuthorityToExistingUser() {
    //given
    final User user =  userRepository.findOne(userRepository.save(createRandomUser()).getId());
    final Authority authority = authorityRepository.save(createRandomAuthority());

    //when
    User changedUser = userRepository.findOne(userService.changeAuthority(user, authority).getId());

    //then
    assertThat(changedUser)
           .isNotNull();
    assertThat(changedUser.getAuthorities())
           .containsExactly(authority);
}

//change authority/role looks like this:
public User changeRole(final User user, final Role role) {
    if (user.hasRole(role)) {
        user.removeRole(role);
    } else {
        user.addRole(role);
    }
    return repository.save(user);
}

The "Authority test" passes, but "Role test" fails (change is not being persisted - User still has no Roles, although it has Authority)!

Can you please help me to answer why??
Thank you in advance


Top
 Profile  
 
 Post subject: Re: Two collections with same cascades inconsistend behavior
PostPosted: Wed Jul 05, 2017 5:36 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
There are many things that are not properly in your example:

1. Why do you find the object you just submitted?

Code:
final User user =  userRepository.findOne(.getId());


That should be:

Code:
User user = createRandomUser();
userRepository.save(user);


2. Since you haven't mentioned the code inside user.removeRole(role), you need to make sure you synchronize both sides of the association.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.