有没有办法使用 JPA/hibernate 滚动结果?

发布于 2024-10-02 17:50:00 字数 372 浏览 8 评论 0 原文

我在 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 替代方案?

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);

is there are jpa/hibernate alternative?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

記憶穿過時間隧道 2024-10-09 17:50:00

据我所知,JPA 中没有这方面的标准。

对于 Hibernate,我知道的最接近的替代方案是 Query / ScrollableResults API。从文档中:

10.4。 1.6.可滚动迭代

如果您的 JDBC 驱动程序支持
可滚动的结果集,查询
接口可用于获取
ScrollableResults 对象允许
灵活的查询导航
结果。

Query q = sess.createQuery("select cat.name, cat from DomesticCat cat " +
                            “按猫名排序”);
ScrollableResults cats = q.scroll();
if ( cats.first() ) {

    // 按名字按字母顺序排列的猫列表每页上的第一个名字
    FirstNamesOfPages = new ArrayList();
    做 {
        字符串名称 = cats.getString(0);
        firstNamesOfPages.add(名称);
    }
    while ( cats.scroll(PAGE_SIZE) );

    // 现在获取猫的第一页
    pageOfCats = new ArrayList();    
    cats.beforeFirst();    
    整数i=0;    
    while( ( PAGE_SIZE > i++ ) && cats.next() ) pageOfCats.add( cats.get(1) );

}

猫.close()

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

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

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

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()

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

浮世清欢 2024-10-09 17:50:00

从其他答案来看,JPA 不支持直接滚动,但如果您使用 Hibernate 作为 JPA 实现,您可以

javax.persistence.Query query = entityManager.createQuery("select foo from bar");
org.hibernate.Query hquery = query.unwrap(org.hibernate.Query);
ScrollableResults results = hquery.scroll(ScrollMode.FORWARD_ONLY);

访问底层 Hibernate api 进行滚动,但您可以使用 JPA 查询的所有功能。 (至少对于条件查询,JPA api 具有一些旧 Hibernate api 中没有的功能。)

Judging from the other answers JPA does not support scrolling directly, but if you use Hibernate as JPA implementation you can do

javax.persistence.Query query = entityManager.createQuery("select foo from bar");
org.hibernate.Query hquery = query.unwrap(org.hibernate.Query);
ScrollableResults results = hquery.scroll(ScrollMode.FORWARD_ONLY);

That accesses the underlying Hibernate api for the scrolling but you can use all the features of JPA querying. (At least for criteria queries the JPA api has some features that are not in the old Hibernate api.)

美人迟暮 2024-10-09 17:50:00

当基于 List 实例处理大型项目代码中的大量实体时,
我必须编写一个非常有限的 List 实现,仅支持 Iterator 来浏览 ScrollableResults,而无需使用 List 重构所有服务实现和方法原型>。

这个实现可以在我的 IterableListScrollableResults.java Gist 中找到,

它还会定期从会话中刷新 Hibernate 实体。这是一种使用它的方法,例如,当使用 for 循环将数据库中的所有非存档实体导出为文本文件时:

Criteria criteria = getCurrentSession().createCriteria(LargeVolumeEntity.class);
criteria.add(Restrictions.eq("archived", Boolean.FALSE));
criteria.setReadOnly(true);
criteria.setCacheable(false);
List<E> result = new IterableListScrollableResults<E>(getCurrentSession(),
        criteria.scroll(ScrollMode.FORWARD_ONLY));
for(E entity : result) {
    dumpEntity(file, entity);
}

希望它可以有所帮助

When processing large number of entities in a large project code based on List<E> instances,
I has to write a really limited List implementation with only Iterator support to browse a ScrollableResults without refactoring all services implementations and method prototypes using List<E>.

This implementation is available in my IterableListScrollableResults.java Gist

It also regularly flushes Hibernate entities from session. Here is a way to use it, for instance when exporting all non archived entities from DB as a text file with a for loop:

Criteria criteria = getCurrentSession().createCriteria(LargeVolumeEntity.class);
criteria.add(Restrictions.eq("archived", Boolean.FALSE));
criteria.setReadOnly(true);
criteria.setCacheable(false);
List<E> result = new IterableListScrollableResults<E>(getCurrentSession(),
        criteria.scroll(ScrollMode.FORWARD_ONLY));
for(E entity : result) {
    dumpEntity(file, entity);
}

With the hope it may help

dawn曙光 2024-10-09 17:50:00

在 JPA 中,您可以使用 query.setFirstResult 和 query.setMaxResults

In JPA you can use query.setFirstResult and query.setMaxResults

肩上的翅膀 2024-10-09 17:50:00

使用 Spring Data 也是一种选择。在那里,您可以指定查询并传递“PageRequest”作为参数,在其中指示页面大小和页码:

Page<User> users = repository.findAll(new PageRequest(1, 20));

为此,您需要扩展 PagingAndSortingRepository。

就像对结果进行分页的另一种选择一样。

当然,在底层,它使用 Hibernate、Toplink 或您配置的任何 JPA 实现。

Also using Spring Data would be an option. There you can specify the query and pass, as a parameter, a "PageRequest" in which you indicate the page size and the page number:

Page<User> users = repository.findAll(new PageRequest(1, 20));

For this you need to extend a PagingAndSortingRepository.

Just as another alternative for paging over the results.

Of course, underneath, it's using Hibernate, Toplink or whatever JPA implementation you configure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文