Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is what in the spring documents is referred to as <a href="http://www.springframework.net/doc-latest/reference/html/classic-spring.html#d4e11771" rel="nofollow noreferrer">Classic Hibernate Usage</a>. It is not the currently recommended approach to work with NHibernate, which is described in the <a href="http://www.springframework.net/doc-latest/reference/html/orm.html#orm-hibernate" rel="nofollow noreferrer">chapter on object relational mappers</a>.</p> <p>The "convenient" usage of delegates here is basically done to provide the <code>HibernateTemplate</code> the means to manage a session and hand this managed session over to other custom methods (in this particular case an <a href="http://msdn.microsoft.com/en-us/library/0yw3tz5k.aspx" rel="nofollow noreferrer">anonymous method</a>). (I think it's an implementation of the <a href="http://www.oodesign.com/visitor-pattern.html" rel="nofollow noreferrer">visitor pattern</a>, btw). </p> <p>Using this approach, the classic <code>HibernateTemplate</code> can provide functionality to methods it doesn't "know of", such as opening and closing sessions correctly and participating in transactions. </p> <p>The query is actually being executed by <code>HibernateTemplate.Execute(myMethod)</code>; I imagine it creates and initializes a session for you, does transaction management, executes your method with the managed session and cleans everything up.</p> <p>I've never used <code>HibernateTemplate</code>, but I'm sure it would require mapping files and a <code>SessionFactory</code>, so if this code is hit during execution and doesn't throw any exceptions, the configuration for those has to be there somewhere!</p> <p>With respect to the questions (besides the delegate part) within your posted code: the use of <code>NHibernateTemplate</code> hasn't really got anything to do with it: you can just as well run this code in any piece of code where you've got hold of a valid <code>ISession</code> instance:</p> <ul> <li>the session executes a HQL query; this query is added to the query cache. I've never used <code>SetCacheRegion</code> myself, but apparently it gives you <a href="http://www.nhforge.org/doc/nh/en/index.html#performance-querycache" rel="nofollow noreferrer">"fine-grained control over query cache expiration policies"</a>.</li> <li><a href="https://stackoverflow.com/a/3214264/322283"><code>query.UniqueResult</code></a></li> <li><a href="https://stackoverflow.com/questions/3295976/what-does-nhibernateutil-initialize-do"><code>NHibernateUtil.Initialize</code></a></li> </ul>
    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