休眠:如何选择最后N行按ID的升序排列? (在单个查询中) [英] Hibernate: how to select last N rows in ascending order of id? (in single query)
问题描述
Hibernate版本:5.2
我试图用子查询来做,并使用setMaxResults(int)。
session.createQuery(
FROM(SELECT * FROM tickets ORDER BY id DESC limit 3)sub ORDER BY id ASC
);
但是,HQL子查询只能出现在select或where子句中,并且限制不能使用在hibernate中。
如何在hibernate中完成它?
更新 - 清楚地说明
例如,有10个数据项从id = 1到id = 10。
我想通过仅查询一个查询 + 来选择最后3个数据,而不用进一步处理数据。
数据库的结果是id = 8到id = 10
谢谢。
查询setMaxResults(int maxResult)$ h2_lin>解决方案 c $ c>来实现它。通常用于分页。
有机会的,您可以在此处下降第一条记录,因此您只能使用查询setMaxResults(int maxResult )
。
Hibernate version: 5.2
I am trying to use subqueries to do, and use setMaxResults(int).
session.createQuery(
"FROM ( SELECT * FROM tickets ORDER BY id DESC limit 3) sub ORDER BY id ASC"
);
However, HQL subqueries can occur only in the select or where clauses, and limit can't be used in hibernate.
How can I do it in hibernate?
Update - To make it clear
For eg, there are 10 data entries from id=1 to id=10.
I want to select last 3 data in ascending order of id by only one query + without further data processing.
The result from db would be id=8 to id=10
Thank You.
解决方案 You can use Query setFirstResult(int startPosition)
, Query setMaxResults(int maxResult)
to implement it. Typically used in the pagination.
Opportunistic, you can get the first 3 record descendingly here, so you can only use the Query setMaxResults(int maxResult)
.
这篇关于休眠:如何选择最后N行按ID的升序排列? (在单个查询中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!