Call Stored Procedure from Nhibernate

This may not the most efficient way to call stored procedure form Nhibernate using projects. But in my situation it first .In general when we uses  Nhibrnate there is almost no need to use. In my case I am mapping my application with Legacy Database so i need sometimes to call Stored Procedures from Nhibernate (though iI am trying my best to avoid this )..

So here is the simple way to use it pretty quickly :

1. Here is my Stored procedure

2. Here is the code snippet


Nhibernate error :Incorrect syntax near the keyword ‘Function’.

In our database sometimes we have column names which are actually the database keywords . If we want to access such columns from Nhibernate session  , it throws error message “Incorrect syntax near the word :’SOME DB KEYWORD’


Mapping :Before :

Mapping :After

Keeping database base reserve keyword in “[ ]”  brackets will fix the error message.


You can have look on all my previous Nhibernate posts












User Nhibernate with Legacy database

Topday we will talk little bit more about the Nhibernate topic. If you are new to this topic than

you can have look at  previous post

As its well accepted that Nhibernate is getting really (probably) first choice in applcation development
Its really easy to stary with Nhibernate for a new application development. You have more control
on your application  code and its db realtions .
Well  this doesnt go as smooth with Legacy DB as new application.

I have legacy database and it has many relations which could drive tyou crazy like anything .

Here in this post I will present a quick but very useful ways to map you WIERED realtions in Nhibernate using fluent Nhibernate.

Here is my small datamodel to explain this kinda of mapping

Data model



Above mapping will create a mapping for the ToolLangTbl.

IF you notice int he mapping we have used CompositId to map such mappings.

Note :

In such cases always implement GetHashCode and Equal methods as below



If you still have some other wiered database relations and need help to map them ..I can try to help you in it.

Have a nice day ahead !!



“No Persister For” error in Nhibernate

Hello Friends

I was running into some other problem and I wanted to test some thins so I have quickly created a small solution.I did all the required steps and run it ..and voilaaa…

i have this small cute error (because its a really small text 🙂 ) It says “No Persister for :….

I get this error when try to save object or SaveUpdate any object.  After a while I figured out that forgot to make my Mapping class Public.

And it resolve the problem.

My DataModel Class

namespace NHBData.Model


public class Employee


public virtual int EmployeeID { get; set; }

public virtual string  EmaployeeName { get; set; }




Mapping file before :

namespace NHBData.Model.Mapping


class EmployeeMapping :ClassMap



{            Id(x => x.EmployeeID).Unique();

Map(x => x.EmaployeeName);




Mapping File after

namespace NHBData.Model.Mapping


Public class EmployeeMapping :ClassMap



{            Id(x => x.EmployeeID).Unique();

Map(x => x.EmaployeeName);




You can have a look to another annoying error and its solution “LazyLoadException” in nhibernate here


Hope this helps to someone..

Have a nice time ..




Nhibernate Self Reference uni-directional

In my previous post we have seen to implement self reference (Bi-directional) . Today we will quickly see  how to achieve self reference uni-directional.


Lets take a simple scenario of a tree structure . One node can have many Children nodes but any child node will have only one parent.



Now  we do not have any mapping table because we want it unidirectional mapping. In database we have only one table Node . Lets see how to get  complete tree now ..

Above mapping will create the below realtion

You can refer below link for more details :

You can download complete implementation from here

Fluent Nhibernate Database configuration

Below is a quick code to connect sql database in Nhibernate using Fluent Nhibernate .

You can read more here for developing complete application.

static ISessionFactory _sessionFactory = null;
public static ISessionFactory CreateSessionFactory()
_sessionFactory = FluentNHibernate.Cfg.Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(c => c
.Mappings(m => m.FluentMappings.AddFromAssembly(System.Reflection.Assembly.Load(“FluentNhibernateMappingSample”)))
return _sessionFactory;}

private static void BuildSchema(Configuration config)
new SchemaExport(config).Create(true, true);

public static ISession GetNHBSession()
if (_sessionFactory == null)
_sessionFactory = CreateSessionFactory();

return _sessionFactory.OpenSession();

Get List by Id’s in Nhibernate

Hello Folks ,

As we know that we do not need to write separate method to get an entity Object by Id in Nhibernate . It’s already provided by Nhibernate . Many times we have  more than one Id’s and we want to get data object for them …

Well here is a simple probably general method (if you add it to your base repository) for all of your objects (which has “Id” column)

Public IEnumerable Get(IEnumerables ids)


var myCriteria = Session.CreateCriteria().Add(Restrictiona.In(“Id”, ids.ToArray()));

// here you  can put your logic ..

return myCriteria.List();