简体   繁体   English

当我们在hibernate中进行fetch =“join”和lazy =“true”时会发生什么

[英]What happens when we make fetch=“join” and lazy=“true” in hibernate

If we use fetching strategy as "join", a single join query is fired (combining parent and children through one join query) and the default behavior is equivalent to lazy="false". 如果我们将获取策略用作“join”,则会触发单个连接查询(通过一个连接查询组合父级和子级),并且默认行为等同于lazy =“false”。 This means that all the children records would be fetched for the parent at once. 这意味着将立即为父项提取所有子记录。 But, what would be the behavior, if we mark lazy="true"? 但是,如果我们标记lazy =“true”,那么行为会是什么? Since the join query is single, what would be lazily fetched? 由于连接查询是单一的,可以懒得取出什么?

FetchMode Join overrides the lazy property. FetchMode Join会覆盖惰性属性。 It will simple be ignored. 它很容易被忽略。 Should you be interested in a detailed explanation about Fetchmodes, take a look here . 如果您对Fetchmodes的详细解释感兴趣,请看这里 The article describes the Hibernate fetchmodes and the output which they produce. 本文描述了Hibernate fetchmodes及其产生的输出。

With fetch="join" on a collection or single-valued association mapping, you will actually avoid the second SELECT (hence making the association or collection non-lazy), by using just one "bigger" outer (for nullable many-to-one foreign keys and collections) or inner (for not-null many-to-one foreign keys) join SELECT to get both the owning entity and the referenced entity or collection. 通过对集合或单值关联映射使用fetch =“join”,您实际上将避免使用第二个SELECT(因此使关联或集合非惰性),只使用一个“更大”的外部(对于可以为空的多个 - 一个外键和集合)或内部(对于非空多对一外键)连接SELECT以获取拥有实体和引用的实体或集合。 If you use fetch="join" for more than one collection role for a particular entity instance (in "parallel"), you create a Cartesian product (also called cross join) and two (lazy or non-lazy) SELECT would probably be faster. 如果对特定实体实例使用fetch =“join”作为多个集合角色(在“并行”中),则创建一个笛卡尔积(也称为交叉连接),两个(惰性或非惰性)SELECT可能是快点。

Use lazy="true" on , and mappings to enable lazy loading of individual scalar value-typed properties (a somewhat exotic case). 使用lazy =“true”on和mappings来启用单个标量值类型属性的延迟加载(有点奇特的情况)。 Requires bytecode instrumentation of compiled persistent classes for the injection of interception code. 需要使用编译的持久化类的字节码检测来注入拦截代码。 Can be overriden in HQL with FETCH ALL PROPERTIES. 可以使用FETCH ALL PROPERTIES在HQL中覆盖。

From https://community.jboss.org/wiki/AShortPrimerOnFetchingStrategies . 来自https://community.jboss.org/wiki/AShortPrimerOnFetchingStrategies Hope it help 希望它有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM