NHibernate Pitfalls: Fetch and Lazy
This is part of a series of posts about NHibernate Pitfalls. See the entire collection here.
When we use the fetch mode join for an association, we are implicitly using eager fetching, not lazy. Because the SELECT statement for the root entity already includes the association table, it will be loaded at the same time, so, it makes no sense to have fetch=”join” and lazy=”false” for an association; in this case, fetch will take precedence and disable lazy loading. Be aware if your association has lots of entities, and you don’t need it most of the time.
On the other hand, for many to one associations that you don't want to load immediately, you must take other steps: set fetch to select, set lazy to either proxy or no-proxy, and also set the other endpoint entity as lazy, otherwise, no proxy will be generated for it and thus it can't be lazy loaded.