Lazy loading
In the beginning of the chapter we saw that when the Employee
entity was loaded, NHibernate did not load the associated collection entities such as Benefits
and Communities
. That is default behavior of NHibernate. At first it may sound strange, but think what would happen if NHibernate tried loading Benefits
and Communities
along with the Employee
entity. Each Community
instance in the Communities
collection has collection of the Employee
instances associated with it. NHibernate will need to load all those Employee
instances. Each of those Employee
instances will have further Community
instances to load. This cycle will only stop when the entire database is loaded into memory. To avoid situations like this, NHibernate only loads the entity that you asked for with all its primitive properties (or properties which are mapped to columns on the table of the entity) populated. Associated collection entities are loaded only when they are accessed. This feature is called lazy loading...