简体   繁体   中英

nHibernate stored procedure call

I'm quite new to nHibernate ORM. Could someone please help me to understand/resolve the scenario I've below?

I have a stored procedure called getSummaryReport which expects @productID as parameter. This stored procedure joins multiple tables and gives the summary data.

I need to load the data returned from the above stored procedure using nhibernate. Could someone please help me what how it's possible to call stored procedure via nHibernate?

Some of the questions running through my head right now is:

  1. Do I need a mapping xml? If yes what goes on to the mapping xml as I understand there should be physical table for each property. In above case stored procedure is generating brand new object.

  2. How do I call the above stored procedure from within C#?

Thank you again.

Try this, your mapping:-

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <sql-query name="SummaryReport">
    exec getSummaryReport  :productId
  </sql-query>
</hibernate-mapping>

and then use SetResultTransformer...

var results = Session
      .GetNamedQuery("SummaryReport")
      .SetInt32("productId", productId);
      .SetResultTransformer(new AliasToBeanResultTransformer(typeof(YOURCLASS)));
return results.List<YOURCLASS>();

and YOURCLASS is:-

public class YOURCLASS 
{
    public virtual int ProductId { get; set; }
    public virtual string Column1Returned { get; set; }
    public virtual int Column2Returned { get; set; }
            etc..
}

Make sure what ever is returned from your SP is defined in YOURCLASS remembering that your column names and property names have to match exactly as they are CASE sensitive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM