MS Sql 查询到 Hibernate HQL

MS Sql query to Hibernate HQL

如何将以下 sql 转换为 HQL

select max(id) from mytable where id in (select top 10 id from mytable where mycolumn-value = 1234 order by id)

你不能完全做一个 JPQL 等价物,因为不可能在子查询中使用 top(或 limitrownum...每个数据库使用一个不同的) JPQL.

但是你用的是这样的:

select max(mt.id) from mytable mt where mt.id in 
   (select mt2.id from mytable mt2 where mt2.mycolumn-value = 1234 order by mt2.id)

此查询忽略子查询的顶部,不会影响预期结果。