How do you create a Distinct query in HQL
do something like this next time
Criteria crit = (Criteria) session.
createCriteria(SomeClass.class).
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List claz = crit.list();
Here's a snippet of hql that we use. (Names have been changed to protect identities)
String queryString = "select distinct f from Foo f inner join foo.bars as b" +
" where f.creationDate >= ? and f.creationDate < ? and b.bar = ?";
return getHibernateTemplate().find(queryString, new Object[] {startDate, endDate, bar});
It's worth noting that the distinct
keyword in HQL does not map directly to the distinct
keyword in SQL.
If you use the distinct
keyword in HQL, then sometimes Hibernate will use the distinct
SQL keyword, but in some situations it will use a result transformer to produce distinct results. For example when you are using an outer join like this:
select distinct o from Order o left join fetch o.lineItems
It is not possible to filter out duplicates at the SQL level in this case, so Hibernate uses a ResultTransformer
to filter duplicates after the SQL query has been performed.