存储过程在nHibernate中不起作用

时间:2014-05-06 20:33:04

标签: c# sql nhibernate stored-procedures sql-server-2012

目标:
使用NHibernate支持存储过程将数据检索到类中。您使用NHibernate通过使用存储过程来检索数据。

问题:
我检索到一条错误消息:

  

未处理的类型异常   ' NHibernate.Exceptions.GenericADOException'发生在NHibernate.dll

     

其他信息:无法执行查询

     

[exec sp_retrieveAllProductCategory] ​​

     

[SQL:exec sp_retrieveAllProductCategory] ​​

我不知道在这种情况下该怎么做。

信息:

enter image description here

enter image description here

enter image description here

enter image description here     test.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="data_layer" namespace="data_layer">
  <sql-query name="sp_retrieveAllProductCategory">

    <return class="Produkt">
      <return-property column="Produkt_kategori" name="Produkt_kategori" />
      <return-property column="Produkt_kategori_ordningsnummer" name="Produkt_kategori_ordningsnummer" />
    </return>
    exec sp_retrieveAllProductCategory
  </sql-query>
</hibernate-mapping>


----------------------


hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Data Source=SHAREPOINT01;Initial Catalog=Active_system;Integrated Security=True</property>
    <property name="show_sql">false</property>
    <mapping assembly="data_layer"/>
  </session-factory>
</hibernate-configuration>


-------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;


namespace data_layer
{

    public class NhibernateDataProvider
    {

        public NhibernateDataProvider()
        {
            _sessionFactory = new Configuration().Configure().BuildSessionFactory();
            _session = _sessionFactory.OpenSession();
        }


        private ISessionFactory _sessionFactory;
        private ISession _session;




        public IList<Produkt> GetAllEmployee()
        {
            return _session.CreateCriteria<Produkt>().List<Produkt>();
        }


        public void GetNamedQuery()
        {
            IQuery query = _session.GetNamedQuery("sp_retrieveAllProductCategory");


            //             IList<Product> products = query.List<Product>();

            IList<Produkt> products = query.List<Produkt>();



        }

    }

}

-------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace data_layer
{
    public class Produkt
    {
        public virtual string Produkt_kategori { get; set;}
        public virtual int Produkt_kategori_ordningsnummer { get; set;}
    }
}

1 个答案:

答案 0 :(得分:-1)

        IList<Produkt> sss = _session
                    .GetNamedQuery("sp_retrieveAllProductCategory")
                    .SetResultTransformer(
                            Transformers.AliasToBean(typeof(Produkt))).List<Produkt>();

https://www.simple-talk.com/blogs/2013/09/27/nhibernate-and-stored-procedures-in-c/