有没有办法用JPA/休眠方式滚动结果? [英] Is there are way to scroll results with JPA/hibernate?

查看:106
本文介绍了有没有办法用JPA/休眠方式滚动结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Toplink中发现了一些提示

I found some hint in Toplink

Query query = em.createQuery("SELECT e FROM Employee e ORDER BY e.lastName ASC, e.firstName ASC");
query.setHint("eclipselink.cursor.scrollable", true);
ScrollableCursor scrollableCursor = (ScrollableCursor)query.getSingleResult();
List<Employee> emps = scrollableCursor.next(10);

是否有jpa/hibernate替代方案?

is there are jpa/hibernate alternative?

推荐答案

据我所知,JPA中没有针对此的标准.

To my knowledge, there is nothing standard in JPA for that.

使用Hibernate,我知道的最接近的替代方法是Query/

With Hibernate, the closest alternative I'm aware of would be the Query / ScrollableResults APIs. From the documentation:

10.4. 1.6.可滚动迭代

如果JDBC驱动程序支持 可滚动的ResultSets,查询 界面可用于获取 ScrollableResults对象,它允许 灵活的查询导航 结果.

10.4.1.6. Scrollable iteration

If your JDBC driver supports scrollable ResultSets, the Query interface can be used to obtain a ScrollableResults object that allows flexible navigation of the query results.

Query q = sess.createQuery("select cat.name, cat from DomesticCat cat " +
                            "order by cat.name");
ScrollableResults cats = q.scroll();
if ( cats.first() ) {

    // find the first name on each page of an alphabetical list of cats by name
    firstNamesOfPages = new ArrayList();
    do {
        String name = cats.getString(0);
        firstNamesOfPages.add(name);
    }
    while ( cats.scroll(PAGE_SIZE) );

    // Now get the first page of cats
    pageOfCats = new ArrayList();    
    cats.beforeFirst();    
    int i=0;    
    while( ( PAGE_SIZE > i++ ) && cats.next() ) pageOfCats.add( cats.get(1) );

}

cats.close()

请注意,打开的数据库连接 并且为此需要光标 功能.使用 setMaxResult()/setFirstResult()(如果您) 需要离线分页功能.

Note that an open database connection and cursor is required for this functionality. Use setMaxResult()/setFirstResult() if you need offline pagination functionality.

这篇关于有没有办法用JPA/休眠方式滚动结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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