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".
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.
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.
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.
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});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With