Hi,
Assume I have the following Groovy class (or the equivalent in Java)
class User {
Long id
String name
}
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.
Update
The following query has been suggested
select min(user.id), user.name
from User user
group by user.name
having count(user.name) > 1
However, there are a few problems with this:
- It doesn't actually return the User objects, just their id and name
- If there are 3 users with the same name, it will only return the id of one of them, whereas I want all 3
- It may not work on MySQL, which is the RDBMS I'm using.
Thanks, Don