tags:

views:

168

answers:

2

I'm having a hard time trying to get my stored procedure works with NHibernate. The data returned from the SP does not correspond to any database table.

This is my mapping file:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DomainModel" namespace="DomainModel.Entities">

    <sql-query name="DoSomething">
        <return class="SomeClass">
            <return-property name="ID" column="ID"/>
        </return>
        exec [dbo].[sp_doSomething]
    </sql-query>

</hibernate-mapping>

Here is my domain class:

namespace DomainModel.Entities
{
    public class SomeClass
    {
        public SomeClass()
        {
        }
        public virtual Guid ID
        {
            get;
            set;
        }
    }
}

When I run the code, it fails with

Exception Details: NHibernate.HibernateException: Errors in named queries: {DoSomething}

at line 80

Line 78:             config.Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NHibernate.config"));            
Line 79: 
Line 80:             g_sessionFactory = config.BuildSessionFactory();       

When I debug into NHibernate code, it seems that SomeClass is not added to the persister dictionary because there isn't a class mapping (only sql-query) defined in hbm.xml. And later on in CheckNamedQueries function, it is not able to find the persistor for SomeClass.

I've checked all the obvious things (e.g. make hbm as an embedded resource) and my code isn't too much different from other samples I found on the web, but somehow I just can't get it working. Any idea how I can resolve this issue?

+1  A: 

Well, where is your class mapping for SomeClass?

You still need to map it. Read http://nhforge.org/doc/nh/en/index.html#querysql-load.

Diego Mijelshon
A: 

Look at using a class mapping with a subselect block. I found this in the Java documentation but maybe it will work for .Net too.

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html (scroll down to section 5.1.3)

nw