Chapter 21. Improving performance

21.1. Fetching strategies

A fetching strategy is the strategy NHibernate will use for retrieving associated objects if the application needs to navigate the association. Fetch strategies may be declared in the O/R mapping metadata, or overridden by a particular HQL or Criteria query.

NHibernate defines the following fetching strategies:

  • Join fetching - NHibernate retrieves the associated instance or collection in the same SELECT, using an OUTER JOIN.

  • Select fetching - a second SELECT is used to retrieve the associated entity or collection. Unless you explicitly disable lazy fetching by specifying lazy="false", this second select will only be executed when you actually access the association.

  • Subselect fetching - a second SELECT is used to retrieve the associated collections for all entities retrieved in a previous query or fetch. Unless you explicitly disable lazy fetching by specifying lazy="false", this second select will only be executed when you actually access the association.

  • "Extra-lazy" collection fetching - individual elements of the collection are accessed from the database as needed. NHibernate tries not to fetch the whole collection into memory unless absolutely needed (suitable for very large collections)

  • Batch fetching - an optimization strategy for select fetching - NHibernate retrieves a batch of entity instances or collections in a single SELECT, by specifying a list of primary keys or foreign keys.

NHibernate also distinguishes between:

  • Immediate fetching - an association, collection or attribute is fetched immediately, when the owner is loaded.

  • Lazy collection fetching - a collection is fetched when the application invokes an operation upon that collection. (This is the default for collections.)

  • Proxy fetching - a single-valued association is fetched when a method other than the identifier getter is invoked upon the associated object.

We have two orthogonal notions here: when is the association fetched, and how is it fetched (what SQL is used). Don't confuse them! We use fetch to tune performance. We may use lazy to define a contract for what data is always available in any detached instance of a particular class.

21.1.1. Working with lazy associations

By default, NHibernate uses lazy select fetching for collections and lazy proxy fetching for single-valued associations. These defaults make sense for almost all associations in almost all applications.

However, lazy fetching poses one problem that you must be aware of. Access to a lazy association outside of the context of an open NHibernate session will result in an exception. For example:

IDictionary<string, int> permissions;
using (var s = sessions.OpenSession())
using (Transaction tx = s.BeginTransaction())
    User u = s.CreateQuery("from User u where u.Name=:userName")
        .SetString("userName", userName).UniqueResult<User>();
    permissions = u.Permissions;


int accessLevel = permissions["accounts"];  // Error!

Since the permissions collection was not initialized when the ISession was closed, the collection will not be able to load its state. NHibernate does not support lazy initialization for detached objects. The fix is to move the code that reads from the collection to just before the transaction is committed.

Alternatively, we could use a non-lazy collection or association, by specifying lazy="false" for the association mapping. However, it is intended that lazy initialization be used for almost all collections and associations. If you define too many non-lazy associations in your object model, NHibernate will end up needing to fetch the entire database into memory in every transaction!

On the other hand, we often want to choose join fetching (which is non-lazy by nature) instead of select fetching in a particular transaction. We'll now see how to customize the fetching strategy. In NHibernate, the mechanisms for choosing a fetch strategy are identical for single-valued associations and collections.

21.1.2. Tuning fetch strategies

Select fetching (the default) is extremely vulnerable to N+1 selects problems, so we might want to enable join fetching in the mapping document:

<set name="Permissions" 
    <key column="userId"/>
    <one-to-many class="Permission"/>
<many-to-one name="Mother" class="Cat" fetch="join"/>

The fetch strategy defined in the mapping document affects:

  • retrieval via Get() or Load()

  • retrieval that happens implicitly when an association is navigated

  • ICriteria queries

  • HQL queries if subselect fetching is used

No matter what fetching strategy you use, the defined non-lazy graph is guaranteed to be loaded into memory. Note that this might result in several immediate selects being used to execute a particular HQL query.

Usually, we don't use the mapping document to customize fetching. Instead, we keep the default behavior, and override it for a particular transaction, using left join fetch in HQL. This tells NHibernate to fetch the association eagerly in the first select, using an outer join. In the ICriteria query API, you would use Fetch().

If you ever feel like you wish you could change the fetching strategy used by Get() or Load(), simply use a ICriteria query, for example:

User user = session.CreateCriteria(typeof(User))
    .Fetch(SelectMode.Fetch, "Permissions")
    .Add( Expression.Eq("Id", userId) )

(This is NHibernate's equivalent of what some ORM solutions call a "fetch plan".)

A completely different way to avoid problems with N+1 selects is to use the second-level cache, or to enable batch fetching.

21.1.3. Single-ended association proxies

Lazy fetching for collections is implemented using NHibernate's own implementation of persistent collections. However, a different mechanism is needed for lazy behavior in single-ended associations. The target entity of the association must be proxied. NHibernate implements lazy initializing proxies for persistent objects using runtime bytecode enhancement.

By default, NHibernate generates proxies (at startup) for all persistent classes and uses them to enable lazy fetching of many-to-one and one-to-one associations.

The mapping file may declare an interface to use as the proxy interface for that class, with the proxy attribute. By default, NHibernate uses a subclass of the class. Note that the proxied class must implement a non-private default constructor. We recommend this constructor for all persistent classes!

There are some gotchas to be aware of when extending this approach to polymorphic classes, eg.

<class name="Cat" proxy="Cat">
    <subclass name="DomesticCat">

Firstly, instances of Cat will never be castable to DomesticCat, even if the underlying instance is an instance of DomesticCat:

// instantiate a proxy (does not hit the db)
Cat cat = session.Load<Cat>(id);
// hit the db to initialize the proxy
if ( cat.IsDomesticCat ) {
    DomesticCat dc = (DomesticCat) cat; // Error!

Secondly, it is possible to break proxy ==.

// instantiate a Cat proxy
Cat cat = session.Load<Cat>(id);
DomesticCat dc =
        // acquire new DomesticCat proxy!
Console.WriteLine(cat == dc); // false

However, the situation is not quite as bad as it looks. Even though we now have two references to different proxy objects, the underlying instance will still be the same object:

cat.Weight = 11.0;  // hit the db to initialize the proxy
Console.WriteLine( dc.Weight );  // 11.0

Third, you may not use a proxy for a sealed class or a class with any non-overridable public members.

Finally, if your persistent object acquires any resources upon instantiation (eg. in initializers or default constructor), then those resources will also be acquired by the proxy. The proxy class is an actual subclass of the persistent class.

These problems are all due to fundamental limitations in .NET's single inheritance model. If you wish to avoid these problems your persistent classes must each implement an interface that declares its business methods. You should specify these interfaces in the mapping file. eg.

<class name="CatImpl" proxy="ICat">
    <subclass name="DomesticCatImpl" proxy="IDomesticCat">

where CatImpl implements the interface ICat and DomesticCatImpl implements the interface IDomesticCat. Then proxies for instances of ICat and IDomesticCat may be returned by Load() or Enumerable(). (Note that List() does not usually return proxies.)

ICat cat = session.Load<CatImpl>(catid);
using(var iter = session
    .CreateQuery("from CatImpl as cat where cat.Name='fritz'")
    ICat fritz = iter.Current;

Relationships are also lazily initialized. This means you must declare any properties to be of type ICat, not CatImpl.

Certain operations do not require proxy initialization

  • Equals(), if the persistent class does not override Equals()

  • GetHashCode(), if the persistent class does not override GetHashCode()

  • The identifier getter method

NHibernate will detect persistent classes that override Equals() or GetHashCode().

21.1.4. Initializing collections and proxies

A LazyInitializationException will be thrown by NHibernate if an uninitialized collection or proxy is accessed outside of the scope of the ISession, ie. when the entity owning the collection or having the reference to the proxy is in the detached state.

Sometimes we need to ensure that a proxy or collection is initialized before closing the ISession. Of course, we can alway force initialization by calling cat.Sex or cat.Kittens.Count, for example. But that is confusing to readers of the code and is not convenient for generic code.

The static methods NHibernateUtil.Initialize() and NHibernateUtil.IsInitialized() provide the application with a convenient way of working with lazily initialized collections or proxies. NHibernateUtil.Initialize(cat) will force the initialization of a proxy, cat, as long as its ISession is still open. NHibernateUtil.Initialize( cat.Kittens ) has a similar effect for the collection of kittens.

Another option is to keep the ISession open until all needed collections and proxies have been loaded. In some application architectures, particularly where the code that accesses data using NHibernate, and the code that uses it are in different application layers or different physical processes, it can be a problem to ensure that the ISession is open when a collection is initialized. There are two basic ways to deal with this issue:

  • In a web-based application, a HttpModule can be used to close the ISession only at the very end of a user request, once the rendering of the view is complete (the Open Session in View pattern). Of course, this places heavy demands on the correctness of the exception handling of your application infrastructure. It is vitally important that the ISession is closed and the transaction ended before returning to the user, even when an exception occurs during rendering of the view. See the NHibernate Wiki for examples of this "Open Session in View" pattern.

  • In an application with a separate business tier, the business logic must "prepare" all collections that will be needed by the web tier before returning. This means that the business tier should load all the data and return all the data already initialized to the presentation/web tier that is required for a particular use case. Usually, the application calls NHibernateUtil.Initialize() for each collection that will be needed in the web tier (this call must occur before the session is closed) or retrieves the collection eagerly using a NHibernate query with a FETCH clause or a SelectMode.Fetch in ICriteria. This is usually easier if you adopt the Command pattern instead of a Session Facade.

  • You may also attach a previously loaded object to a new ISession with Merge() or Lock() before accessing uninitialized collections (or other proxies). No, NHibernate does not, and certainly should not do this automatically, since it would introduce ad hoc transaction semantics!

Sometimes you don't want to initialize a large collection, but still need some information about it (like its size) or a subset of the data.

You can use a collection filter to get the size of a collection without initializing it:

s.CreateFilter(collection, "select count(*)").UniqueResult<long>()

The CreateFilter() method is also used to efficiently retrieve subsets of a collection without needing to initialize the whole collection:

s.CreateFilter(lazyCollection, "").SetFirstResult(0).SetMaxResults(10).List<Entity>();

21.1.5. Using batch fetching

NHibernate can make efficient use of batch fetching, that is, NHibernate can load several uninitialized proxies if one proxy is accessed (or collections). Batch fetching is an optimization of the lazy select fetching strategy. There are two ways you can tune batch fetching: on the class and the collection level.

Batch fetching for classes/entities is easier to understand. Imagine you have the following situation at runtime: You have 25 Cat instances loaded in an ISession, each Cat has a reference to its Owner, a Person. The Person class is mapped with a proxy, lazy="true". If you now iterate through all cats and call cat.Owner on each, NHibernate will by default execute 25 SELECT statements, to retrieve the proxied owners. You can tune this behavior by specifying a batch-size in the mapping of Person:

<class name="Person" batch-size="10">...</class>

NHibernate will now execute only three queries, the pattern is 10, 10, 5.

You may also enable batch fetching of collections. For example, if each Person has a lazy collection of Cats, and 10 persons are currently loaded in the ISession, iterating through all persons will generate 10 SELECTs, one for every call to person.Cats. If you enable batch fetching for the Cats collection in the mapping of Person, NHibernate can pre-fetch collections:

<class name="Person">
    <set name="Cats" batch-size="3">

With a batch-size of 3, NHibernate will load 3, 3, 3, 1 collections in four SELECTs. Again, the value of the attribute depends on the expected number of uninitialized collections in a particular Session.

Batch fetching of collections is particularly useful if you have a nested tree of items, ie. the typical bill-of-materials pattern. (Although a nested set or a materialized path might be a better option for read-mostly trees.)

Note: if you set default_batch_fetch_size in configuration, NHibernate will configure the batch fetch optimization for lazy fetching globally. Batch sizes specified at more granular level take precedence.

21.1.6. Using subselect fetching

If one lazy collection or single-valued proxy has to be fetched, NHibernate loads all of them, re-running the original query in a subselect. This works in the same way as batch-fetching, without the piecemeal loading.

21.2. The Second Level Cache

A NHibernate ISession is a transaction-level cache of persistent data. It is possible to configure a cluster or process-level (ISessionFactory-level) cache on a class-by-class and collection-by-collection basis. You may even plug in a clustered cache. Be careful. Caches are never aware of changes made to the persistent store by another application (though they may be configured to regularly expire cached data). In NHibernate 1.x the second level cache does not work correctly in combination with distributed transactions.

The second level cache requires the use of transactions, be it through transaction scopes or NHibernate transactions. Interacting with the data store without an explicit transaction is discouraged, and will not allow the second level cache to work as intended.

By default, NHibernate uses HashtableCache for process-level caching. You may choose a different implementation by specifying the name of a class that implements NHibernate.Cache.ICacheProvider using the property cache.provider_class.

Table 21.1. Cache Providers

CacheProvider classTypeCluster SafeQuery Cache Supported
Hashtable (not intended for production use)NHibernate.Cache.HashtableCacheProvidermemory yes
ASP.NET Cache (System.Web.Cache)NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCachememory yes
Prevalence CacheNHibernate.Caches.Prevalence.PrevalenceCacheProvider, NHibernate.Caches.Prevalencememory, disk yes

21.2.1. Cache mappings

The <cache> element of a class or collection mapping has the following form:

    usage="read-write|nonstrict-read-write|read-only|never"          (1)
    region="RegionName"                                              (2)

usage specifies the caching strategy: read-write, nonstrict-read-write, read-only or never


region (optional, defaults to the class or collection role name) specifies the name of the second level cache region

Alternatively (preferably?), you may specify <class-cache> and <collection-cache> elements in hibernate.cfg.xml.

The usage attribute specifies a cache concurrency strategy.

21.2.2. Strategy: read only

If your application needs to read but never modify instances of a persistent class, a read-only cache may be used. This is the simplest and best performing strategy. Its even perfectly safe for use in a cluster.

<class name="Eg.Immutable" mutable="false">
    <cache usage="read-only"/>

21.2.3. Strategy: read/write

If the application needs to update data, a read-write cache might be appropriate. This cache strategy should never be used if serializable transaction isolation level is required. You should ensure that the transaction is completed when ISession.Close() or ISession.Disconnect() is called. If you wish to use this strategy in a cluster, you should ensure that the underlying cache implementation supports locking. The built-in cache providers do not.

<class name="eg.Cat" .... >
    <cache usage="read-write"/>
    <set name="Kittens" ... >
        <cache usage="read-write"/>

21.2.4. Strategy: nonstrict read/write

If the application only occasionally needs to update data (ie. if it is extremely unlikely that two transactions would try to update the same item simultaneously) and strict transaction isolation is not required, a nonstrict-read-write cache might be appropriate. When using this strategy you should ensure that the transaction is completed when ISession.Close() or ISession.Disconnect() is called.

21.2.5. Strategy: never

By default, without a cache configuration, entities are not cacheable. But they may still be referenced in cacheable queries, which results will then be cached according to the values these non cacheable entities have. So, their data may be indirectly cached through cacheable queries.

By using the cache strategy never, such indirect caching of these entities data will be forbidden by NHibernate. Setting as cacheable a query referencing entities with strategy never will be treated as an error by default. Alternatively, the query.throw_never_cached setting can be set to false: instead of raising an error, it will disable the query cache on such queries, and log a warning.

The following table shows which providers are compatible with which concurrency strategies.

Table 21.2. Cache Concurrency Strategy Support

Hashtable (not intended for production use)yesyesyes

Refer to Chapter 27, NHibernate.Caches for more details.

21.3. Managing the caches

Whenever you pass an object to Save(), Update() or SaveOrUpdate() and whenever you retrieve an object using Load(), Get(), List(), or Enumerable(), that object is added to the internal cache of the ISession.

When Flush() is subsequently called, the state of that object will be synchronized with the database. If you do not want this synchronization to occur or if you are processing a huge number of objects and need to manage memory efficiently, the Evict() method may be used to remove the object and its collections from the first-level cache.

IEnumerable<Cat> cats = sess
    .CreateQuery("from Eg.Cat as cat")
    .List<Cat>(); //a huge result set
foreach (Cat cat in cats)

NHibernate will evict associated entities automatically if the association is mapped with cascade="all" or cascade="all-delete-orphan".

The ISession also provides a Contains() method to determine if an instance belongs to the session cache.

To completely evict all objects from the session cache, call ISession.Clear()

For the second-level cache, there are methods defined on ISessionFactory for evicting the cached state of an instance, entire class, collection instance or entire collection role.

//evict a particular Cat
sessionFactory.Evict(typeof(Cat), catId);
//evict all Cats
//evict a particular collection of kittens
sessionFactory.EvictCollection("Eg.Cat.Kittens", catId);
//evict all kitten collections

21.4. The Query Cache

Query result sets may also be cached. This is only useful for queries that are run frequently with the same parameters. To use the query cache you must first enable it:

<property name="cache.use_query_cache">true</property>>

This setting causes the creation of two new cache regions - one holding cached query result sets (NHibernate.Cache.StandardQueryCache), the other holding timestamps of the most recent updates to queryable tables (UpdateTimestampsCache). Those region names will be prefixed by the cache region prefix if cache.region_prefix setting is configured.

If you use a cache provider handling an expiration for cached entries, you should set the UpdateTimestampsCache region expiration to a value greater than the expiration of query cache regions. (Or disable its expiration.) Otherwise the query cache may yield stale data.

Note that the query cache does not cache the state of any entities in the result set; it caches only identifier values and results of value type. So the query cache should always be used in conjunction with the second-level cache.

Most queries do not benefit from caching, so by default queries are not cached. To enable caching, call IQuery.SetCacheable(true). This call allows the query to look for existing cache results or add its results to the cache when it is executed.

If you require fine-grained control over query cache expiration policies, you may specify a named cache region for a particular query by calling IQuery.SetCacheRegion().

var blogs = sess.CreateQuery("from Blog blog where blog.Blogger = :blogger")
    .SetEntity("blogger", blogger)

If the query should force a refresh of its query cache region, you may call IQuery.SetForceCacheRefresh() to true. This is particularly useful in cases where underlying data may have been updated via a separate process (i.e., not modified through NHibernate) and allows the application to selectively refresh the query cache regions based on its knowledge of those events. This is a more efficient alternative to eviction of a query cache region via ISessionFactory.EvictQueries().

21.5. Understanding Collection performance

We've already spent quite some time talking about collections. In this section we will highlight a couple more issues about how collections behave at runtime.

21.5.1. Taxonomy

NHibernate defines three basic kinds of collections:

  • collections of values

  • one to many associations

  • many to many associations

This classification distinguishes the various table and foreign key relationships but does not tell us quite everything we need to know about the relational model. To fully understand the relational structure and performance characteristics, we must also consider the structure of the primary key that is used by NHibernate to update or delete collection rows. This suggests the following classification:

  • indexed collections

  • sets

  • bags

All indexed collections (maps, lists, arrays) have a primary key consisting of the <key> and <index> columns. In this case collection updates are usually extremely efficient - the primary key may be efficiently indexed and a particular row may be efficiently located when NHibernate tries to update or delete it.

Sets have a primary key consisting of <key> and element columns. This may be less efficient for some types of collection element, particularly composite elements or large text or binary fields; the database may not be able to index a complex primary key as efficiently. On the other hand, for one to many or many to many associations, particularly in the case of synthetic identifiers, it is likely to be just as efficient. (Side-note: if you want SchemaExport to actually create the primary key of a <set> for you, you must declare all columns as not-null="true".)

<idbag> mappings define a surrogate key, so they are always very efficient to update. In fact, they are the best case.

Bags are the worst case. Since a bag permits duplicate element values and has no index column, no primary key may be defined. NHibernate has no way of distinguishing between duplicate rows. NHibernate resolves this problem by completely removing (in a single DELETE) and recreating the collection whenever it changes. This might be very inefficient.

Note that for a one-to-many association, the "primary key" may not be the physical primary key of the database table - but even in this case, the above classification is still useful. (It still reflects how NHibernate "locates" individual rows of the collection.)

21.5.2. Lists, maps, idbags and sets are the most efficient collections to update

From the discussion above, it should be clear that indexed collections and (usually) sets allow the most efficient operation in terms of adding, removing and updating elements.

There is, arguably, one more advantage that indexed collections have over sets for many to many associations or collections of values. Because of the structure of an ISet, NHibernate doesn't ever UPDATE a row when an element is "changed". Changes to an ISet always work via INSERT and DELETE (of individual rows). Once again, this consideration does not apply to one to many associations.

After observing that arrays cannot be lazy, we would conclude that lists, maps and idbags are the most performant (non-inverse) collection types, with sets not far behind. Sets are expected to be the most common kind of collection in NHibernate applications. This is because the "set" semantics are most natural in the relational model.

However, in well-designed NHibernate domain models, we usually see that most collections are in fact one-to-many associations with inverse="true". For these associations, the update is handled by the many-to-one end of the association, and so considerations of collection update performance simply do not apply.

21.5.3. Bags and lists are the most efficient inverse collections

Just before you ditch bags forever, there is a particular case in which bags (and also lists) are much more performant than sets. For a collection with inverse="true" (the standard bidirectional one-to-many relationship idiom, for example) we can add elements to a bag or list without needing to initialize (fetch) the bag elements! This is because IList.Add() must always succeed for a bag or IList (unlike an ISet). This can make the following common code much faster.

Parent p = sess.Load<Parent>(id);
    Child c = new Child();
    c.Parent = p;
    p.Children.Add(c);  //no need to fetch the collection!

21.5.4. One shot delete

Occasionally, deleting collection elements one by one can be extremely inefficient. NHibernate isn't completely stupid, so it knows not to do that in the case of an newly-empty collection (if you called list.Clear(), for example). In this case, NHibernate will issue a single DELETE and we are done!

Suppose we add a single element to a collection of size twenty and then remove two elements. NHibernate will issue one INSERT statement and two DELETE statements (unless the collection is a bag). This is certainly desirable.

However, suppose that we remove eighteen elements, leaving two and then add thee new elements. There are two possible ways to proceed:

  • Delete eighteen rows one by one and then insert three rows

  • Remove the whole collection (in one SQL DELETE) and insert all five current elements (one by one)

NHibernate isn't smart enough to know that the second option is probably quicker in this case. (And it would probably be undesirable for NHibernate to be that smart; such behaviour might confuse database triggers, etc.)

Fortunately, you can force this behaviour (ie. the second strategy) at any time by discarding (ie. dereferencing) the original collection and returning a newly instantiated collection with all the current elements. This can be very useful and powerful from time to time.

Of course, one-shot-delete does not apply to collections mapped inverse="true".

21.6. Batch updates

NHibernate supports batching SQL update commands (INSERT, UPDATE, DELETE) with the following limitations:

  • the NHibernate's drive used for your RDBMS may not supports batching,

  • since the implementation uses reflection to access members and types in System.Data assembly which are not normally visible, it may not function in environments where necessary permissions are not granted,

  • optimistic concurrency checking may be impaired since ADO.NET 2.0 does not return the number of rows affected by each statement in the batch, only the total number of rows affected by the batch.

Update batching is enabled by setting adonet.batch_size to a non-zero value.

21.7. Query batch

This functionality allows you to execute several queries in one round-trip against the database server. A simple use case is executing a paged query while also getting the total count of results, in a single round-trip. Here is an example:

using NHibernate.Multi;


IQueryBatch queries = s.CreateQueryBatch()
        s.CreateQuery("from Item i where i.Id > ?")
         .SetInt32(0, 50).SetFirstResult(10))
        s.CreateQuery("select count(*) from Item i where i.Id > :id")
         .SetInt32("id", 50));
IList<Item> items = queries.GetResult<Item>(0);
long count = queries.GetResult<long>(1).Single();

The results are got by index, ordered according to the order of queries added to the query batch. Instead of relying on this ordering, a key can be associated with each query for later retrieval:

using NHibernate.Multi;


var queries = s.CreateQueryBatch()
    .Add("list", s.Query<Item>().Where(i => i.Id > 50))
    .Add("count", s.Query<Item>().Where(i => i.Id > 50), q => q.Count());
var count = queries.GetResult<int>("count").Single();
var items = queries.GetResult<Item>("list");

The namespace NHibernate.Multi has to be imported since most query batch methods are extension methods.

Criteria queries are also supported by the query batch:

var queries = s.CreateQueryBatch()
         .Add(Expression.Gt("Id", 50))
         .Add(Expression.Gt("Id", 50))
var items = queries.GetResult<Item>(0);
var count = queries.GetResult<long>(1).Single();

You can add ICriteria or DetachedCriteria to the query batch. In fact, using DetachedCriteria in this fashion has some interesting implications.

DetachedCriteria customersCriteria = AuthorizationService.GetAssociatedCustomersQuery();
IQueryBatch queries = session.CreateQueryBatch()

IList<Customer> customers = queries.GetResult<Customer>(0);
IList<Policy> policies = queries.GetResult<Policy>(1);

We get a query that represents the customers we can access, and then we can utilize this query further in order to perform additional logic (getting the policies of the customers we are associated with), all in a single database round-trip.

The query batch also supports QueryOver and sql queries. All kind of queries can be mixed in the same batch.

using NHibernate.Multi;


var queries = s.CreateQueryBatch()
    .Add("queryOverList", s.QueryOver<Item>().Where(i => i.Category == "Food"))
        s.CreateSQLQuery("select count(*) as itemCount from Item i where i.Category = :cat")
         .AddScalar("itemCount", NHibernateUtil.Int64)
         .SetString("cat", "Food"));
var count = queries.GetResult<long>("sqlCount").Single();
var items = queries.GetResult<Item>("queryOverList");

Second level cache is supported by the query batch. Queries flagged as cacheable will be retrieved from the cache if already cached, otherwise their results will be put in the cache.

using NHibernate.Multi;


var queries = s.CreateQueryBatch()
         .Where(i => i.Id > 50)
         .WithOptions(o => o.SetCacheable(true)))
        s.CreateQuery("select count(*) from Item i where i.Id > :id")
         .SetInt32("id", 50)
var count = queries.GetResult<long>("count").Single();
var items = queries.GetResult<Item>("list");

Multi query is executed by concatenating the queries and sending the query to the database as a single string. This means that the database should support returning several result sets in a single query. Otherwise each query will be individually executed instead.

The first GetResult call triggers execution of the whole query batch, which then stores all results. Later calls only retrieve the stored results. A query batch can be re-executed by calling its Execute method. Once executed, no new query can be added to the batch.

Note that the database server is likely to enforce a limit on the maximum number of parameters in a query, in which case the limit applies to the query batch as a whole. Queries using in with a large number of arguments passed as parameters may easily exceed this limit. For example, SQL Server has a limit of 2,100 parameters per round-trip, and will throw an exception executing this query:

int[] allEmployeesId  = ...; // 1,500 items
var queries = s.CreateQueryBatch()
        s.CreateQuery("from Employee e where e.Id in :empIds")
         .SetParameterList("empIds", allEmployeesId)
        s.CreateQuery("select count(*) from Employee e where e.Id in :empIds")
         .SetParameterList("empIds", allEmployeesId));
queries.Execute(); // will throw an exception from SQL Server

An interesting usage of the query batch is to load several collections of an object in one round-trip, without an expensive cartesian product (blog * users * posts).

Blog blog = s.CreateQueryBatch()
        s.CreateQuery("select b from Blog b left join fetch b.Users where b.Id = :id")
         .SetInt32("id", 123))
        s.CreateQuery("select b from Blog b left join fetch b.Posts where b.Id = :id")
         .SetInt32("id", 123))

You can also add queries as future queries to a query batch:

using NHibernate.Multi;


var queries = s.CreateQueryBatch();
var list = queries.AddAsFuture(s.Query<Item>().Where(i => i.Id > 50)));
var countValue = queries.AddAsFutureValue<long>(
    s.CreateQuery("select count(*) from Item i where i.Id > :id")
     .SetInt32("id", 50));
var count = countValue.Value;
var items = list.GetEnumerable();

Futures built from a query batch are executed together the first time the result of one of them is accessed. They are independent of futures obtained directly from the queries.

21.8. Future results

Queries can be converted to future results instead of being directly executed. Future results are not evaluated till one gets executed. At that point, all defined future results are evaluated in one single round-trip to the database.

Future results are an alternative to using Section 21.7, “Query batch”. They avoid the need to explicitly regroup queries, but they also hide which queries will get executed: any pending future results of the session will be batched together, no matter where they were defined, included out-of-scope pending future results.

Future results are obtained by calling Future or FutureValue methods of a HQL, Criteria, QueryOver or SQL query. For LINQ queries, the methods are named ToFuture and ToFutureValue, see Section 18.4, “Future results” for an example.

// Define queries
IFutureEnumerable<Cat> cats =
    session.CreateQuery("from Cat c where c.Color = :color")
        .SetString("color", "black")
IFutureValue<int> catCount =
// Execute them
foreach(Cat cat in cats.GetEnumerable())
    // Do something
if (catCount.Value > 10)
    // Do something

In the above example, accessing catCount.Value does not trigger a round-trip to the database: it has been evaluated with cats.GetEnumerable() call. If instead catCount.Value was accessed first, it would have executed both future results and cats.GetEnumerable() would not have triggered a round-trip to the database.

As showcased in the previous example, Future allows to get a future enumerable result, and FutureValue is meant to obtain a single value result.

Note: in NHibernate v5.1 and previous versions, Criteria/QueryOver future results were batched separately. Since NHibernate v5.2, they are batched with other querying API future results.