了解Hibernate中的@BatchSize [英] Understanding @BatchSize in Hibernate

查看:182
本文介绍了了解Hibernate中的@BatchSize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hibernate文档给出@BatchSize的一些信息为:

lockquote
@BatchSize指定一个批量大小,用于通过标识符获取这个
类的实例。尚未加载的实例在
a时间(默认为1)下被加载为批量大小。


我不清楚什么是这个注释的目的,当我们需要使用这个。有些人可以帮我理解何时使用这个注解。

解决方案

使用批量提取,Hibernate可以加载多个未初始化的代理代理被访问。批量抓取是对懒惰选择抓取策略的优化。有两种方法可以配置批次抓取:在类级别和集合级别。



类/实体的批量抓取更容易理解。考虑下面的例子:在运行时,你有一个会话中加载了25个Cat实例,每个Cat都有一个对它的所有者的引用Person。 Person类映射了一个代理,lazy =true。如果你现在遍历所有的猫,并调用getOwner(),默认情况下,Hibernate将执行25条SELECT语句来检索代理所有者。您可以通过在Person的映射中指定批处理大小来调整此行为:

 < class name =Personbatch -size = 10 > ...< /类> 

Hibernate现在只会执行三个查询:模式为10,10,5。


The Hibernate documentation gives some information at @BatchSize as :

@BatchSize specifies a "batch size" for fetching instances of this class by identifier. Not yet loaded instances are loaded batch-size at a time (default 1).

I am not clear on what is the purpose of this annotation, when we need to use this. Can some please help me in understanding when to use this annotation.

解决方案

Using batch fetching, Hibernate can load several uninitialized proxies if one proxy is accessed. Batch fetching is an optimization of the lazy select fetching strategy. There are two ways you can configure batch fetching: on the class level and the collection level.

Batch fetching for classes/entities is easier to understand. Consider the following example: at runtime you have 25 Cat instances loaded in a Session, and each Cat has a reference to its owner, a Person. The Person class is mapped with a proxy, lazy="true". If you now iterate through all cats and call getOwner() on each, Hibernate will, by default, execute 25 SELECT statements to retrieve the proxied owners. You can tune this behavior by specifying a batch-size in the mapping of Person:

<class name="Person" batch-size="10">...</class>

Hibernate will now execute only three queries: the pattern is 10, 10, 5.

这篇关于了解Hibernate中的@BatchSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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