Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>"If we want to access the order line items (after the session has been closed) we get an exception. Since the session is closed NHibernate cannot lazily load the order line items for us. We can show this behavior with the following test method"</p> </blockquote> <pre><code>[Test] [ExpectedException(typeof(LazyInitializationException))] public void Accessing_customer_of_order_after_session_is_closed_throws() { Order fromDb; using (ISession session = SessionFactory.OpenSession()) fromDb = session.Get&lt;Order&gt;(_order.Id); // trying to access the Customer of the order, will throw exception // Note: at this point the session is already closed string name = fromDb.Customer.CompanyName; } </code></pre> <blockquote> <p>"Eagerly loading with the NHibernateUtil class If you know you need have access to related objects of the order entity you can use the NHibernateUtil class to initialize the related objects (that is: to fetch them from the database)."</p> </blockquote> <pre><code>[Test] public void Can_initialize_customer_of_order_with_nhibernate_util() { Order fromDb; using (ISession session = SessionFactory.OpenSession()) { fromDb = session.Get&lt;Order&gt;(_order.Id); NHibernateUtil.Initialize(fromDb.Customer); } Assert.IsTrue(NHibernateUtil.IsInitialized(fromDb.Customer)); Assert.IsFalse(NHibernateUtil.IsInitialized(fromDb.OrderLines)); } </code></pre> <p>Reference: <a href="http://nhibernate.info/doc/howto/various/lazy-loading-eager-loading.html" rel="nofollow">http://nhibernate.info/doc/howto/various/lazy-loading-eager-loading.html</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload