title: Hibernate: find duplicates
body: <p>Assume I have the following Groovy class (or the equivalent in Java)</p>
  
  <pre><code>class User  {
      Long id
      String name
  }
  </code></pre>
  
  <p>I would like to write a Hibernate query (either HQL or Criteria) which returns all the users that have at least one other user with the same name.</p>
  
  <p><strong>Update</strong></p>
  
  <p>The following query has been suggested</p>
  
  <pre><code>select min(user.id), user.name
  from User user
  group by user.name
  having count(user.name) &gt; 1
  </code></pre>
  
  <p>However, there are a few problems with this:</p>
  
  <ul>
  <li>It doesn't actually return the User objects, just their id and name</li>
  <li>If there are 3 users with the same name, it will only return the id of one of them, whereas I want all 3</li>
  <li>It may not work on MySQL, which is the RDBMS I'm using.</li>
  </ul>
  
  <p>Thanks,
  Don</p>