Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write mappings for a stored procedure

There is an excellent post on how to map return values for a stored procedure call here: http://elegantcode.com/2008/11/23/populating-entities-from-stored-procedures-with-nhibernate/

The mapping in this example has been done through hbm files.

I am trying to use the latest version of Nhibernate (3.2) where we can do mapping through code. I really want to find out the C# code that would create a mapping like below:

<sql-query name="GetProductsByCategoryId">
    <return class="Product">
      <return-property column="ProductID" name="Id" />
      <return-property column="ProductName" name="Name" />
      <return-property column="SupplierID" name="Supplier" />
      <return-property column="CategoryID" name="Category" />
      <return-property column="QuantityPerUnit" name="QuantityPerUnit" />
      <return-property column="UnitPrice" name="UnitPrice" />
      <return-property column="UnitsInStock" name="UnitsInStock" />
      <return-property column="UnitsOnOrder" name="UnitsOnOrder" />
      <return-property column="ReorderLevel" name="ReorderLevel" />
      <return-property column="Discontinued" name="Discontinued" />
    </return>
    exec dbo.GetProductsByCategoryId :CategoryId
</sql-query>
like image 932
dreamerkumar Avatar asked Nov 14 '22 13:11

dreamerkumar


1 Answers

To be honest I never tried it, by you should take a look to the extension method AddNamedQuery(..): you call it from you Configuration instance (NHibernate.Cfg namespace)).

Some examples on the NHibernate test project.

By the way, you can mix the new 3.2 mapping-by-code and xml one. Start to look at this question;

like image 193
Michele Lepri Avatar answered Nov 16 '22 03:11

Michele Lepri