Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a Distinct query in HQL

Is there a way to create a Distinct query in HQL. Either by using the "distinct" keyword or some other method. I am not sure if distinct is a valid keywork for HQL, but I am looking for the HQL equivalent of the SQL keyword "distinct".

like image 688
Mike Pone Avatar asked Nov 04 '08 23:11

Mike Pone


People also ask

Can we use distinct in HQL query?

Using distinct in the HQL Query We can notice that the distinct keyword was not only used by Hibernate but also included in the SQL query. We should avoid this because it's unnecessary and will cause performance issues.

What is a distinct query?

The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.

Can we use distinct in JPQL?

DISTINCT with JPQL entity queries. The DISTINCT keyword has a different purpose when it comes to entity queries. Without using DISTINCT , the JPA specification states that the returning entities resulting from a parent-child JOIN might contain object reference duplicates.


1 Answers

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}); 
like image 169
Feet Avatar answered Sep 29 '22 06:09

Feet