在Hibernate中使用PostgreSQL的'select distinct on'

9

Hibernate在查询中是否支持SELECT DISTINCT?

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

有没有一种方法可以为该查询创建Hibernate标准?

4
因为这是PostgreSQL特有的功能,我猜答案会是“否”。 - Craig Ringer
2
不,我想你必须使用本地 SQL。 - Maurice Perry
如果您无法使用本地查询,则可以创建一个视图,并将其映射到 Hibernate 实体,就像普通表一样。 - NumberFour
1个回答

7

由于这里有很多“怀疑”和“猜测”,所以当我遇到这个问题时,我会明确回答:

不,Hibernate不支持DISTINCT ON查询。

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]

在 Hibernate 4.3.9-final 下测试通过。

请注意,这不适用于普通的 DISTINCT 查询。 请参阅 https://dev59.com/3nVC5IYBdhLWcg3wihmv...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接