Sunday, November 27, 2011

break into HQL to empower NHibernate's eager loading

I stole the following from here as an example of how to eagerly load with NHibernate (and also how to break into HQL while returning a specific object type):

[Test]

public void Can_eagerly_load_order_aggregate_with_hql_query()

{

   Order fromDb;

   using (ISession session = SessionFactory.OpenSession())

   {

      string sql = "from Order o" +

            " inner join fetch o.OrderLines" +

            " inner join fetch o.Customer" +

            " where o.Id=:id";

      fromDb = session.CreateQuery(sql)

            .SetGuid("id", _order.Id)

            .UniqueResult<Order>();

   }

   Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer));

   Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.OrderLines));

}

 
 

I also found this which suggests that the average implementation of Hibernate/NHibernate is going to use lazy loading by default.

I'm writing this post in follow up to this post.

No comments:

Post a Comment