Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your question actually boils down to object caching and reuse. If you load a Foo object from one session, then can you keep hold of it and then at a later point in time lazy load its Bar property?</p> <p>Each ISession instance is designed to represent a unit of work, and comes with a first level cache that will allow you to retrieve an object multiple times within that unit of work yet only have a single database hit. It is not thread-safe, and should definitely not be used as a static object in a WinForms application.</p> <p>If you want to use an object when the session under which it was loaded has been disposed, then you need to associate it with a new session using Session.SaveOrUpdate(object) or Session.Update(object).</p> <p>You can find all of this explained in <a href="https://www.hibernate.org/hib_docs/nhibernate/html/transactions.html" rel="nofollow noreferrer">chapter 10 of the Hibernate documentation</a>.</p> <p>If this seems inefficient, then look into second-level caching. This is provided at ISessionFactory level - your session factory can be static, and if you enable second-level caching this will effectively build an in-memory cache of much of your data. Second-level caching is only appropriate if there is no underlying service updating your data - if all database updates go via NHibernate, then it is safe.</p> <p><strong>Edit in light of code posted</strong></p> <p>Your session usage is at the wrong level - you are using it for a single database get, rather than a unit of work. In this case, your <code>GetById</code> method should take in a session which it uses, and the session instance should be managed at a higher level. Alternatively, your <code>PersonRepository</code> class should manage the session if you prefer, and you should instantiate and dispose an object of this type for each unit of work.</p> <pre><code>public T GetById(int id) { return m_session.Get&lt;T&gt;(id); } using (var repository = new PersonRepository()) { var person = repository.GetById(1); var contactInfo = person.ContactInfo; } // make sure the repository Dispose method disposes the session. </code></pre> <p>The error message you are getting is because there is no longer a session to use to lazy load the collection - you've already disposed it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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