title: Hibernate Many-to-many association: left hand side collection contains elements, but right hand side collection is empty
body: <p>I got a problem with a many to many association in my persistence layer. My scenario is the following:</p>
  
  <p>A user can has several roles and a role can have several user attached to it. During the tests I encountered a strange behavior. I created role object and several user objects. The role was set to each of the users. After this the users were saved using a DAO. Then one of the user gets loaded to check whether he got the role that was passed to him before saving the user object. Calling <code>getRoles()</code> on the user shows that the role was set correctly. </p>
  
  <p>To check whether the inverse direction also works the role object gets loaded from the database using a role DAO. But calling <code>getUsers()</code> on the role object just returns an empty set, although it should contain all the users with this role. </p>
  
  <p>I double checked the database table but everything seems all right. User, role and user_role table were all filled correctly. </p>
  
  <p><strong>So why doesn't the role object contain any user?</strong> </p>
  
  <p>I'm using Hibernate and Spring with the following classes. </p>
  
  <p><strong>User class</strong></p>
  
  <pre><code>@Entity
  @Table
  public class User extends BusinessObject {
  
      ... 
  
      // Option 1
      @ManyToMany(fetch = FetchType.LAZY,
                  cascade = CascadeType.ALL,
                  targetEntity=Role.class)
      @JoinTable(name= "user_role",
                 joinColumns = {@JoinColumn(name="user_id")},
                 inverseJoinColumns = {@JoinColumn(name="role_id")})  
  
      // Option 2
      @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
      @JoinTable(name= "user_role", 
                     joinColumns = {@JoinColumn(name="user_id")},
             inverseJoinColumns = {@JoinColumn(name="role_id")})
      private Set&lt;Role&gt; roles = new HashSet&lt;Role&gt;();      
  
      ... 
  }
  </code></pre>
  
  <p><strong>Role class</strong></p>
  
  <pre><code>@Entity
  @Table
  public class Role extends BusinessObject {
      ...
  
      // Option 1
      @ManyToMany(fetch = FetchType.LAZY, 
                  cascade = CascadeType.ALL,
                  mappedBy= "roles",
                  targetEntity = User.class)
  
      // Option 2
      @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
      @JoinTable(name= "user_role", 
                     joinColumns = {@JoinColumn(name="role_id")},
                     inverseJoinColumns = {@JoinColumn(name="user_id")})
      private Set&lt;User&gt; users = new HashSet&lt;User&gt;();          
  
      ... 
  }
  </code></pre>
  
  <p>To test I'm using the following code in a JUnit test class. </p>
  
  <pre><code>@Test
  public void test(){     
      Transaction trans = sessionFactory.getCurrentSession().beginTransaction();
  
      Role userAdminRole = new Role();
      userAdminRole.setName(RoleName.USER_ADMIN);
      Role userRole = new Role();
      userRole.setName(RoleName.USER);
  
      User user1 = new User();
      user1.setEmail("user1@user.de");        
      user1.getRoles().add(userAdminRole);
      user1.getRoles().add(userRole);
      userDao.save(user1);
  
      User user2 = new User();
      user2.setEmail("user2@user.de");
      user2.getRoles().add(role);
      userDao.save(user2);
  
      User user3 = new User();
      user3.setEmail("user3@user.de");
      user3.getRoles().add(role);
      userDao.save(user3);            
  
      trans.commit();     
  
      User loadedUser = userDao.load(user1.getId());
  
              // Tests passes
      Assert.assertNotNull(loadedUser);
      Assert.assertEquals(user1, loadedUser);
  
      Set&lt;Role&gt; roles = loadedUser.getRoles();        
  
              // Tests passes
      Assert.assertEquals(2, roles.size());
  
      Role loadedUserAdminRole = roleDao.findByName(RoleName.USER_ADMIN);
      Set&lt;User&gt; users = loadedUserAdminRole.getUsers();
  
      // Test fails: Count is 0 instead of 3 !!!!!!!
      Assert.assertEquals(3, users.size());
  }  
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Sorry I forgot to mention one thing. When I tested the code I of course didn't mark the many to many association twice in each class file. Instead I used either option 1 or option 2 in each class file. </p>