I'm working on setting up NHibernate for a project and I have a few queries that, due to their complexity, we will be leaving as stored procedures. I'd like to be able to use NHibernate to call the sprocs, but have run into an error I can't figure out. Since I'm using Fluent NHibernate I'm using mixed mode mapping as recommended here. However, when I run the app I get a "Named query not known: AccountsGetSingle" exception and I can't figure out why. I think I might have a problem with my HBM mapping since I'm not very familiar with using them but I'm not sure.
My NHibernate configuration code is:
private ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005
.ConnectionString((conn => conn.FromConnectionStringWithKey("CIDB")))
.ShowSql())
.Mappings(m =>
{
m.HbmMappings.AddFromAssemblyOf<Account>();
m.FluentMappings.AddFromAssemblyOf<Account>();
})
.BuildSessionFactory();
}
My hbm.xml file is:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="AccountsGetSingle">
<return alias="Account" class="Core, Account"></return>
exec AccountsGetSingle
</sql-query>
</hibernate-mapping>
And the code where I am calling the sproc looks like this:
public Account Get()
{
return _conversation.Session
.GetNamedQuery("AccountsGetSingle")
.UniqueResult<Account>();
}
Any thoughts or ideas would be appreciated. Thanks.
Update: @kibbled_bits's suggestion get me the end result that I'm looking for (Being able to call a Stored Procedure from NHibernate), but I still don't know why the approach I have listed above doesn't work. I'm still curious as to why since it might provide valuable insight into future problems.
When I have to use stored procedures (which only occurs when I'm forced to). I much rather use the following method to execute them:
The first parameter to .SetInt32/DateTime is just the ordinal position of the parameter.
I got the same error message and what solved it for me was to ensure that my hbm.xml file property "Build action" was set to "Embedded Resource", so you may want to give it another try.
I have been caught out by this error a number of times.
Two other issues can cause this.
Not adding the hbm mapping.
In fluent I have the following.
Where I miss the view class out (which pairs up with an hbm file with the mapping information). I get the error.
When I added the new view 'typeof(CompanyAtoZListingView)' it worked fine.
Also do check in the hbm file to make sure the parameters are correct.