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

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

Mapping

 

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

{

EmployeeMapping()

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

Map(x => x.EmaployeeName);

}

}

}

Mapping File after

namespace NHBData.Model.Mapping

{

Public class EmployeeMapping :ClassMap

{

EmployeeMapping()

{            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.

Scenario

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.

Node.cs

NodeMap.cs

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 :

http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/05/14/how-to-map-a-tree-in-nhibernate.aspx

You can download complete implementation from here

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();

}

 

Fluent Nhibernate : Create Many-to-Many Relationship table

Scenario :

I have 2 objects User and Group . One User can have many Groups and , many Groups can have many users . To implement this relation , I need a UserGroupMapping table which has reference of UserID and GroupID.

UserGroup Object

public class UserGroup    {

public UserGroup()

{

Users = new List<Users>();

}

public virtual int Id { get; set; }

public virtual string Value { get; set; }
public virtual IList<Users> Users { get; set; }

}

Users Object

public class Users    {

public Users()

{

UserGroups = new List();

}

public virtual int Id { get; set; }

public virtual string FirstName{ get; set; }

public virtual string LastName{ get; set; }
public virtual IList UserGroups { get; set; }

}


User mapping

public class UsersMap:ClassMap

{

public UsersMap()

{

Table(“Users”);

Id(x => x.Id,”UserID”).GeneratedBy.Identity();

Map(x => x.FirstName);

Map(x => x.LastName);

HasManyToMany<UserGroups>(x => x.Groups).Table(“UserGroupMapping“)

.ParentKeyColumn(“UserID”)

.ChildKeyColumn(“GroupID”);

Map(x => x.CreatedOn);

}

}

Group Mapping

public class UserGroupMap :ClassMap

{

public UserGroupMap()

{

Table(“Groups”);

Id(x => x.Id,”GroupID”).GeneratedBy.Identity();

Map(x => x.Value);

HasManyToMany<Users>(x => x.Users).Table(“UserGroupMapping“)

.ParentKeyColumn(“GroupID”)

.ChildKeyColumn(“UserID”);

References(x => x.CreatedBy).Not.LazyLoad();

Map(x => x.CreatedOn);

Map(x => x.UpdatedOn).Update();

}

}

 

Above mapping will create a table UserGroupMapping.  You can see my previous post on self reference of an object .