NHibernate: Mapping Application Domain
Mapping Fetching Strategy
- NHibernate Mapping Application Domain: Mapping Class Inheritance
- NHibernate Mapping Application Domain: Mapping Class Associations
- NHibernate Mapping Application Domain: Mapping Class Collections
- NHibernate Mapping Application Domain: Mapping Fetching Strategy
Summary
NHibernate lets you specify a default fetching strategy in the XML mapping file, with the ability to then override it at runtime in code.
NHibernate provides four different fetching strategies for any assocation: Immediate, Lazy, Eager or Batch.
Immediate Fetching
- The associated object is fetched immediately, either through a sequential database read or a cache lookup.
- Only an efficient fetching strategy when the associated objects are cached already.
Lazy Fetching
- The associated object or collection is fetched "lazily" when the object is first accessed.
- This results in a new request to the database, unless the associated object is cached.
- This is the default fetching strategy configured for NHibernate 2.1.
Eager Fetching
- The associated object or collection is fetched together with the owning object, using a SQL outer join, and no further database request is required.
- NHibernate is limited to one eagerly fetched collection per mapped persistent class.
Batch Fetching
- Used to improve the performance of lazy fetching by retrieving a batch of objects or collections when a lazy association is accessed.
The fetching strategy can be overwritten for a particular association using the outer-join attribute for an association, as follows:
For single point associations
| Property Attribute | Attribute Description |
|---|---|
| outer-join="auto" | Default. Dependant on fetching strategy specified for the class. If class is configured for lazy fetching than NHibernate fetches the association lazily, otherwise NHibernate fetches the association eagerly. |
| outer-join="true" | NHibernate fetches the association eagerly using an outer-join, even if the class has been configured for lazy fetching. |
| outer-join="false" | NHibernate never fetches the association using an outer join, even when lazy fetching is disabled. |
For collections
| Property Attribute | Attribute Description |
|---|---|
| outer-join="true" | NHibernate fetches the collection eagerly using an outer join. |
| lazy="true" | NHibernate fetches the collection lazily, when it is first accessed. This option is preferred for collection mappings, and is the default in HNibernate 2.1. |
The correct fetching strategy minimizes the number of SQL statements that have to be executed by lazily, eagerly, or batch-fetching objects.
Printer Friendly Version
Add to Favourites
DotNet Kicks
Digg
del.icio.us
Live Favourites
ma.gnolia
reddit
Slashdot
Technorati
Yahoo!