title: How to get users by their roles using hibernate criteria in Spring
body: <p>I am using Spring with Hibernate in my project. I want to get users by a particular role using Hibernate criteria. My User model is connected with Role model using <code>@ManyToMany</code> annotation:</p>
  
  <pre><code>@ManyToMany(fetch = FetchType.EAGER)
  @Fetch(FetchMode.SELECT)
  @JoinTable(
          name = "user_role",
          joinColumns = {@JoinColumn(name = "user_id")},
          inverseJoinColumns = @JoinColumn(name = "role_id")
  )
  public Set&lt;Role&gt; getRoles() {
      return roles;
  }
  </code></pre>
  
  <p>and here is my criteria in my <code>UserDaoHibernate</code>:</p>
  
  <pre><code>public List&lt;User&gt; getUsersByRole(List&lt;Role&gt; roles) {
      List users = getSession().createCriteria(User.class)
              .add(Restrictions.in("roles", roles)).list();
      return users;
  }
  </code></pre>
  
  <p>I am getting DataIntegrityViolationException when executing this criteria. Am i missing something or is there any correct way of getting users by their roles?</p>