有没有办法使用 JPA/hibernate 滚动结果?
我在 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 替代方案?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
据我所知,JPA 中没有这方面的标准。
对于 Hibernate,我知道的最接近的替代方案是
Query
/ScrollableResults
API。从文档中: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:从其他答案来看,JPA 不支持直接滚动,但如果您使用 Hibernate 作为 JPA 实现,您可以
访问底层 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
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.)
当基于
List
实例处理大型项目代码中的大量实体时,我必须编写一个非常有限的 List 实现,仅支持
Iterator
来浏览ScrollableResults
,而无需使用List
重构所有服务实现和方法原型>。这个实现可以在我的 IterableListScrollableResults.java Gist 中找到,
它还会定期从会话中刷新 Hibernate 实体。这是一种使用它的方法,例如,当使用
for
循环将数据库中的所有非存档实体导出为文本文件时:希望它可以有所帮助
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 aScrollableResults
without refactoring all services implementations and method prototypes usingList<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:With the hope it may help
在 JPA 中,您可以使用 query.setFirstResult 和 query.setMaxResults
In JPA you can use query.setFirstResult and query.setMaxResults
使用 Spring Data 也是一种选择。在那里,您可以指定查询并传递“PageRequest”作为参数,在其中指示页面大小和页码:
为此,您需要扩展 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:
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.