java 了解 Hibernate hibernate.max_fetch_depth 和 hibernate.default_batch_fetch_size

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25146492/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:26:59  来源:igfitidea点击:

Understanding Hibernate hibernate.max_fetch_depth and hibernate.default_batch_fetch_size

javahibernate

提问by Chaitanya

The Hibernate documenationgives some of the Hibernate configuration properties. Among them,

休眠documenation给出了一些Hibernate配置属性。其中,

hibernate.max_fetch_depth

Sets a maximum "depth" for the outer join fetch tree for single-ended associations (one-to-one, many-to-one). A 0 disables default outer join fetching. e.g. recommended values between 0 and 3

hibernate.default_batch_fetch_size

Sets a default size for Hibernate batch fetching of associations. e.g. recommended values 4, 8, 16

hibernate.max_fetch_depth

为单端关联(一对一、多对一)的外连接获取树设置最大“深度”。0 禁用默认的外连接获取。例如 0 到 3 之间的推荐值

hibernate.default_batch_fetch_size

为 Hibernate 批量获取关联设置默认大小。例如推荐值 4、8、16

I am new to Hibernate, can someone please help me in understanding this with an example.

我是 Hibernate 的新手,有人可以通过一个例子帮助我理解这一点。

Thanks in advance.

提前致谢。

回答by Maarten Winkels

max_fetch_depth: Imagine a Person and an Address Entity. Each person lives at one address (very simple system), but many people might live at the same address. In the object model a Person would probably have an address property. This would be mapped as many-to-one (as the doc says). Now when fetching a Person from the database, hibernate encounters this property. In the database it's a column with a foreign key to the Address table. To fetch the associated object, a join to this table can be used. The resulting data would be used to populate an Address object that would be set on the address property on the person. In this sense Hibernate traverses the object graph when fetching an object. Now what if Address had a property City? This would also be many-to-one and would lead to a join by the same logic. What if City had a property Country? Same thing. Doing many joins would be bad for performance. At some point it would be better to do a separate select, get the data from a cache or inject a proxy. This configuration property determines how many associations hibernate will traverse by join when fetching data.

max_fetch_depth:想象一个人和一个地址实体。每个人住在一个​​地址(非常简单的系统),但很多人可能住在同一个地址。在对象模型中,一个人可能有一个地址属性。这将被映射为多对一(如文档所述)。现在,当从数据库中获取 Person 时,hibernate 会遇到此属性。在数据库中,它是一个带有指向 Address 表的外键的列。要获取关联的对象,可以使用与该表的连接。结果数据将用于填充一个 Address 对象,该对象将在此人的 address 属性上设置。从这个意义上说,Hibernate 在获取对象时会遍历对象图。现在如果 Address 有一个属性 City 呢?这也将是多对一的,并且会通过相同的逻辑导致连接。如果 City 有一个财产 Country 会怎样?一样。进行多次连接对性能不利。在某些时候,最好进行单独的选择,从缓存中获取数据或注入代理。这个配置属性决定了在获取数据时,hibernate 将通过 join 遍历多少个关联。

default_batch_fetch_size: This is a very low level property that determines how many rows Hibernate will request the JDBC driver to fetch/load when querying a collection association. If you would query all cities in a country (previous example) then by loading the data in batches over the JDBC connection the process of getting the data across and into memory as objects will be more streamlined. In comparison to the ring it will take for the query to execute our for the application to process the data, however, it is usually insignificant. Leaving the configuration property to default value will most often be correct.

default_batch_fetch_size:这是一个非常低级别的属性,它确定在查询集合关联时,Hibernate 将请求 JDBC 驱动程序获取/加载多少行。如果您要查询一个国家/地区的所有城市(前面的示例),那么通过 JDBC 连接批量加载数据,将数据作为对象跨入内存并将其作为对象传入内存的过程将更加简化。与环相比,查询执行我们的应用程序来处理数据所需的时间,然而,它通常是微不足道的。将配置属性保留为默认值通常是正确的。