HQL select range of results

孤者浪人 提交于 2019-12-11 03:28:04

问题


What is the correct/efficient way to retrieve a sub set of results from hibernate?

Assuming that products is a table containing a 3000 records.

Session session = SessionFactorys.getSessionFactory().openSession();
Query query = session.createQuery("from products p");
List result = query.list().subList(30, 40);     
session.disconnect();

The code above dose not seem to be very efficient is there a better way, I am trying to find something like.

Query query = session.createQuery("from products p range(30,40)");

回答1:


Use query.setFirstResult() and query.setMaxResults() but be aware that this of course depends on the ordering of your result set.

Query query = session.createQuery("from products p");
query.setFirstResult(30);
query.setMaxResults(10);
List result = query.list();



回答2:


Look at setFirstResult and setMaxResults in the javadoc of Query.

Note that these are applied to the rows returned by the JDBC calls, and not to the entities returned by the query. In general, that's equivalent. But it's not if your query fetches a *ToMany association.



来源:https://stackoverflow.com/questions/8463747/hql-select-range-of-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!