gpt4 book ai didi

hibernate XML 映射 : Lazy False or Fetch Select?

转载 作者:行者123 更新时间:2023-12-03 11:48:01 26 4
gpt4 key购买 nike

我有一个简单的问题。我在我们的项目中找到了这个 Hibernate 配置:

<many-to-one name="employee" 
class="com.myapp.Employee"
cascade="merge"
lazy="false"
fetch="select">
<column name="employee_id"
sql-type="bigint"
not-null="true"/>
</many-to-one>

没有 获取=“选择”意思是“延迟加载所有集合和实体”基于 Fetching Strategies .但是通过写 懒惰=“假”意思是不要懒加载。所以上面的配置说:“禁用延迟加载。启用延迟加载。”实际上,这意味着该属性是延迟加载的吗?

所以我可以将该配置缩短为:
<many-to-one name="employee" 
class="com.myapp.Employee"
cascade="merge"
fetch="select">
<column name="employee_id"
sql-type="bigint"
not-null="true"/>
</many-to-one>

但不是 获取=“选择”默认模式?所以实际上,我可以声明相同的配置:
<many-to-one name="employee" 
class="com.myapp.Employee"
cascade="merge">
<column name="employee_id"
sql-type="bigint"
not-null="true"/>
</many-to-one>

我对么?错误的?想法?谢谢

如果我想启用延迟加载,我必须添加 懒惰=“真”并删除 懒惰=“假” ?

最佳答案

我认为获取模式和获取时间是不完全重叠的概念。
Lazy="true|false"控制关联是急切加载还是按需加载。
fetch="select|subselect|join|batch"控制该实体或集合在需要加载时如何加载。

所以,回答你的问题,有 fetch="select"方法:

"a second SELECT is used to retrieve the associated entity or collection. Unless you explicitly disable lazy fetching by specifying lazy="false", this second select will only be executed when you access the association." (http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html#performance-fetching)



这并不意味着延迟加载被禁用!这是由 lazy="true|false" 控制的旗帜。
lazy="true"fetch="select" Hibernate 将延迟加载集合并使用选择加载它。如果您设置 lazy="false" , 将执行相同的选择,不同之处在于它会急切地执行。希望这是有道理的。

也看看这里: http://community.jboss.org/wiki/AShortPrimerOnFetchingStrategies

关于 hibernate XML 映射 : Lazy False or Fetch Select?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4374528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com