使用 Spring Data 设置获取大小 [英] Set the fetch size with Spring Data
问题描述
对于大型结果集,增加提取大小很重要.关于如何为 Spring 的 JdbcTemplate
设置 fetch 大小已经有很多讨论.但是,我们通常使用 Spring Data 进行数据库访问.假设我们使用 JPA 和 Hibernate 作为提供者,如何设置 Spring Data 查询的获取大小?
For large result sets, it’s important to increase the fetch size. There have been numerous discussions on how to set the fetch size for Spring’s JdbcTemplate
. However, we usually use Spring Data for database access. How can I set the fetch size for a Spring Data query, assuming we use JPA with Hibernate as provider?
推荐答案
这取决于您要设置的内容.如果你想要它全局,只需将它作为一个属性添加到你的persistence.xml(或者你的配置方式EntityManagerFactory
是什么).对于休眠,这意味着添加 hibernate.jdbc.fetch_size
属性.
It depends on what you want to set. If you want it globally simply add it as a property to your persistence.xml (or what your way of configuration the EntityManagerFactory
is). For hibernate that means adding the hibernate.jdbc.fetch_size
property.
<property name="hibernate.jdbc.fetch_size" value="50" />
如果您想为某些查询指定它,请使用来自 JPA 查询对象的查询提示.
If you want to specify it for certain queries use query hints from JPA on the Query object.
TypedQuery<Foo> q = em.createTypedQuery("some hql here", Foo.class);
q.setHint("org.hibernate.fetchSize", "100");
或者在使用 Spring Data JPA 时在接口方法上使用 @QueryHints
注释.可以应用于有和没有 @Query
的两种方法.
Or when using Spring Data JPA use a @QueryHints
annotation on the interface method. Can be applied to both methods with and without @Query
.
@QueryHints(@javax.persistence.QueryHint(name="org.hibernate.fetchSize", value="50"))
List<Foo> findAll();
链接
- Hibernate 文档一个>
- Spring 数据 JPA 参考 |javadoc一个>
- JPA 2 查询提示 javadoc
- 查询提示列表用于休眠
- Hibernate documentation
- Spring Data JPA reference | javadoc
- JPA 2 Query Hints javadoc
- List of query hints for Hibernate
这篇关于使用 Spring Data 设置获取大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!