HelloWorld HelloWorld - 4 months ago 33
SQL Question

Stored Procedure don't work in nHibernate

Goal:

retrieve data into a class with support of a stored procedure with NHibernate. You use NHibernate to retrieve the data by using stored procedure.

Problem:

I retrieve a error message saying:


An unhandled exception of type
'NHibernate.Exceptions.GenericADOException' occurred in NHibernate.dll

Additional information: could not execute query

[ exec sp_retrieveAllProductCategory ]

[SQL: exec sp_retrieveAllProductCategory]


I dont know what to do in this context.

Information:



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;}
}
}

Answer
        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/

Comments