简体   繁体   中英

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". 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"? Since the join query is single, what would be lazily fetched?

FetchMode Join overrides the lazy property. It will simple be ignored. Should you be interested in a detailed explanation about Fetchmodes, take a look here . The article describes the Hibernate fetchmodes and the output which they produce.

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. 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.

Use lazy="true" on , and mappings to enable lazy loading of individual scalar value-typed properties (a somewhat exotic case). Requires bytecode instrumentation of compiled persistent classes for the injection of interception code. Can be overriden in HQL with FETCH ALL PROPERTIES.

From https://community.jboss.org/wiki/AShortPrimerOnFetchingStrategies . Hope it help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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