views:

181

answers:

1

How do you call a stored procedure with nHibernate?

Specifically there are two cases where I am using store procedures: to return a scalar value and to return a set of results mapped to entities.

+4  A: 

The documentation suggests the following for mapping a named query for a stored procedure:

<sql-query name="selectAllEmployments_SP">
  <return alias="emp" class="Employment">
  <return-property name="employee" column="EMPLOYEE"/>
  <return-property name="employer" column="EMPLOYER"/>
  exec selectAllEmployments   //stored procedure call here
</sql-query>

This could be called by using :

IQuery q = sess.GetNamedQuery("selectAllEmployments_SP");

This is discussed in section 13.2.2 (mapping stored procedure) and 9.3.2 (querying named query) of the NHibernate 1.2.0 documentation. https://www.hibernate.org/hib%5Fdocs/nhibernate/1.2/reference/en/html/querysql.html#sp%5Fquery

Michael Gattuso