Some times I want to load an instance of an object which has one or several child collections. Usually I design my object relational mapping to use lazy loading by default. The result of this is that when loading a single instance of a object and accessing its child collections NHibernate spits out at least one additional query per collection. So I have many roudtrips to the server.
Fortuately NHibernate 1.2 offers the new possbility to create a "Multi-Query". The result is (at least for the SQL Server) that only ONE single roundtrip is made to the server and several resultsets are returned.
Let's make an example. We have a Category entity which has two child collections of type Product and Note. That is, any category object can have a list of associated Products and Notes. If I want to display all details of a specific Category on a view, then I have to load the details of the category object and the details of any of the associated products and notes objects. I have at least 3 queries to send to the database.
Now with the following syntax we reduce the roundtrips to the database needed to exactly one:
IList firstResultList = (IList) session.CreateMultiQuery()
.Add("from Category cat left join fetch cat.Products where cat.CategoryID = :id")
.Add("from Category cat left join fetch cat.Notes where cat.CategoryID = :id")
Category item = (Category) firstResultList;