Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql 'select distinct on' in hibernate

Does hibernate has support on select distinct on queries?

SELECT DISTINCT ON (location) location, time, report
FROM weather_reports
ORDER BY location, time DESC;

Is there a way to create hibernate criterias for that query?

like image 786
froi Avatar asked Mar 11 '14 08:03

froi


People also ask

How to use DISTINCT in Hibernate?

You can add the DISTINCT keyword to your query to tell Hibernate to return each Author entity only once. But as you can see in the following log messages, Hibernate also adds the DISTINCT keyword to the SQL query. This is often not intended and might result in an efficient database query.

Can we use distinct in HQL?

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.

Can we use distinct in PostgreSQL?

Removing duplicate rows from a query result set in PostgreSQL can be done using the SELECT statement with the DISTINCT clause. It keeps one row for each group of duplicates. The DISTINCT clause can be used for a single column or for a list of columns.


1 Answers

Since there was all "suspecting" and "guessing" here, when I stumbled across this question, I'll answer it definitely:

No, Hibernate does not support a DISTINCT ON query.

org.hibernate.hql.internal.ast.QuerySyntaxException: 
unexpected token: ON near line 1, column 17
[SELECT DISTINCT ON (location) location, time, report FROM weather_reports ORDER BY location, time DESC]

Tested under Hibernate 4.3.9-final

Please note that this doesn't apply to normal DISTINCT queries. See https://stackoverflow.com/questions/263850...

like image 94
Radall Avatar answered Oct 21 '22 01:10

Radall