Click here to Skip to main content
15,878,430 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Goal:
Retrieve data into a class with support of stored procedure with nhibernate. You use nhibernate to retrieve the data by using stored procedure.

Problem:
I have followed some instructions (the link to the website is below) but it doesn't work because I retrieve a error message that is:

"An unhandled exception of type 'NHibernate.MappingException' occurred in NHibernate.dll"
"Additional information: Named query not known: sp_retrieveAllProductCategory"

Information:
- http://nhforge.org/blogs/nhibernate/archive/2008/11/23/populating-entities-from-stored-procedures-with-nhibernate.aspx
- I'm using VS 2013 and SQL server 2013.


-----------------------------------
XML
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>



-------------------
C#
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;}
    }
}
Posted

1 solution

Hi,


Try this link :https://www.simple-talk.com/blogs/2013/09/27/nhibernate-and-stored-procedures-in-c/[^]

Its very well described

Thanks
Suvabata
 
Share this answer
 
Comments
deelll 7-May-14 8:55am    
It goes better when I tried the new solution based on the link that you have provided. I retrieved another error message that is "{"query did not return a unique result: 6"}". Do you know what I need to do?
Suvabrata Roy 7-May-14 9:44am    
I think you are using SingleOrDefault and query returned multiple result so this error occur
You can change your code to FirstOrDefault I think it should work...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900