简体   繁体   English

nHibernate 存储过程调用

[英]nHibernate stored procedure call

I'm quite new to nHibernate ORM. Could someone please help me to understand/resolve the scenario I've below?我是 nHibernate ORM 的新手。有人可以帮助我理解/解决下面的场景吗?

I have a stored procedure called getSummaryReport which expects @productID as parameter.我有一个名为getSummaryReport的存储过程,它需要@productID作为参数。 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?我需要使用 nhibernate 加载从上述存储过程返回的数据。有人可以帮助我如何通过 nHibernate 调用存储过程吗?

Some of the questions running through my head right now is:现在我脑海中浮现的一些问题是:

  1. Do I need a mapping xml?我需要映射 xml 吗? If yes what goes on to the mapping xml as I understand there should be physical table for each property.如果是的话,据我所知,映射 xml 应该有每个属性的物理表。 In above case stored procedure is generating brand new object.在上述情况下,存储过程正在生成全新的 object。

  2. How do I call the above stored procedure from within C#?如何从 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...然后使用 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.确保从您的 SP 返回的内容在YOURCLASS中定义,记住您的列名和属性名必须完全匹配,因为它们CASE大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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