使用 Spring Data 设置获取大小 [英] Set the fetch size with Spring Data

查看:43
本文介绍了使用 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();

链接

  1. Hibernate 文档
  2. Spring 数据 JPA 参考 |javadoc
  3. JPA 2 查询提示 javadoc
  4. 查询提示列表用于休眠
  1. Hibernate documentation
  2. Spring Data JPA reference | javadoc
  3. JPA 2 Query Hints javadoc
  4. List of query hints for Hibernate

这篇关于使用 Spring Data 设置获取大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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