休眠分页机制 [英] hibernate pagination mechanism

查看:132
本文介绍了休眠分页机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为我的查询使用Hibernate分页(PostgreSQL)

我为我的sql查询设置了setFirstResult(0),setMaxResults(20)。我的代码如下:

  Session session = getSessionFactory()。getCurrentSession(); 
session.beginTransaction();
Query query = session.createQuery(FROM Customers);
query.setFirstResult(0);
query.setMaxResults(20);
列表< T> entities = query.list();
session.getTransaction()。commit();

但是当查看SQL hibernate日志时,我仍然看到完整的sql查询:

  Hibernate:选择customer0_.id为id9_,customer0_.customer_name为dst2_9_,customer0_.addres为tbl_customers中的dst3_9_ customer0_ 

为什么在Hibernate分页SQL查询中没有LIMIT OFFSET?



有没有人了解Hibernate的分页机制?



我猜测Hibernate会选择所有数据,将数据放入Resultset中,然后在Resultset中进行分页,对吗?

解决方案

我在查询和hibernate回调中使用。两者都按预期工作。 Hibernate Query针对给定的First和Max大小之间的结果执行。这里看起来像你传递了SQL而不是HQL来查询。如果是的话,它不应该工作。



- 看我的代码在这里。

  Query query = this.getSession()。createQuery(FROM QueryType); 
query.setFirstResult(0);
query.setMaxResults(20);
List toDelete = query.list();

和日志:

select


(选择
- 所有列名称(不要在此共享)
from
MY_TBL_NAME querytype0_)
其中
rownum< =?


I am trying to use Hibernate pagination for my query (PostgreSQL )

i set setFirstResult(0), setMaxResults(20) for my sql query. My code like below:

Session session = getSessionFactory().getCurrentSession();
session.beginTransaction();
Query query = session.createQuery("FROM Customers");
query.setFirstResult(0);
query.setMaxResults(20);
List<T> entities = query.list();
session.getTransaction().commit();

but when viewing SQL hibernate log, i still see full sql query:

Hibernate: select customer0_.id as id9_, customer0_.customer_name as dst2_9_, customer0_.addres as dst3_9_ from tbl_customers customer0_  

Why there is no LIMIT OFFSET in query of Hibernate pagination SQL log?

Does anyone know about Hibernate pagination mechanism?

I guess that Hibernate will select all data, put data into Resultset, and then paging in Resultset, right?

解决方案

I am using in query and in hibernate call back. both are working as expected. Hibernate Query executes for results in between First and Max size given. Here Seems like you passed SQL not HQL to query. if yes it shouldn't work.

-- See my code here.

        Query query = this.getSession().createQuery("FROM QueryType");
        query.setFirstResult(0);
        query.setMaxResults(20);
        List toDelete = query.list();

and in log:

select * from ( select -- ALL column names. (dont want to share here.) from MY_TBL_NAME querytype0_ ) where rownum <= ?

这篇关于休眠分页机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆