java 理解 Hibernate 中的 @BatchSize
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25210949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding @BatchSize in Hibernate
提问by Chaitanya
The Hibernate documentationgives some information at @BatchSize as :
在Hibernate文档提供了在@BatchSize一些信息:
@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).
@BatchSize 指定用于通过标识符获取此类的实例的“批量大小”。尚未加载的实例一次加载批量大小(默认为 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.
不清楚这个注解的目的是什么,什么时候需要用到这个。有些人可以帮助我理解何时使用此注释。
回答by Aun
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.
使用批量获取,如果访问一个代理,Hibernate 可以加载多个未初始化的代理。批量抓取是对惰性选择抓取策略的优化。您可以通过两种方式配置批量提取:在类级别和集合级别。
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:
类/实体的批量获取更容易理解。考虑以下示例:在运行时,您在一个会话中加载了 25 个 Cat 实例,每个 Cat 都有一个对其所有者 Person 的引用。Person 类使用代理来映射,lazy="true"。如果您现在遍历所有猫并对每只猫调用 getOwner(),Hibernate 将默认执行 25 条 SELECT 语句来检索代理所有者。您可以通过在 Person 的映射中指定批量大小来调整此行为:
<class name="Person" batch-size="10">...</class>
Hibernate will now execute only three queries: the pattern is 10, 10, 5.
Hibernate 现在将只执行三个查询:模式是 10、10、5。
回答by Kanagaraj M
@BatchSize will fetch record from db based on your size. Think you have 100 records in your result and if you are defined batch size as 10 then it will fetch 10 records per db call. Logically it will increase performance.
@BatchSize 将根据您的大小从数据库中获取记录。认为您的结果中有 100 条记录,如果您将批量大小定义为 10,那么每个数据库调用将获取 10 条记录。从逻辑上讲,它会提高性能。