Click here to Skip to main content
Click here to Skip to main content

Execute Stored Procedure in SQL Server using nHibernate

By , 20 Aug 2009
 

Introduction

This article shows how to execute a stored procedure (SP) in Microsoft Visual Studio (C#) using nHibernate.

Background

I was facing a lot of challenges to complete this task. As I was new to nHibernate technology, Googling also did not help me much on this front and I spent a lot of time to get the actual result. Here, I am sharing with you some tips while doing this type of activity.

Useful Tips

Hibernate Config File

Points to remember while writing nHibernate configuration file (*.hbm.xml).

  • All the returned parameters in the stored procedure will be as "return-scalar" with column name and type.
  • Use db aliases instead of db column name in the stored procedure.
  • If your SP has input parameters, then use comma separated ? marks for each.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="GetCustomerOrderHistory"> //code name of your SP
<return-scalar column="ProductName" type="String" /> //all the return parameters 
<return-scalar column="Total" type="Int32" />
exec CustOrderHist :? 
</sql-query>
</hibernate-mapping>

Hibernate Model Class

The following are very important while writing a model class for your hibernate configuration for SP:

  • There will be a property for each returned parameter.
  • There will be only one parametric constructor in your model class and you need to set all private variables with a specific one.
public class HistoricalOrderSummary
{
    public HistoricalOrderSummary(string productName, int totalQuantity) {
    _productName = productName;
    _totalQuantity = totalQuantity;
}

public string ProductName {
    get { return _productName; }
}

public int TotalQuantity {
    get { return _totalQuantity; }
}

private string _productName;

private int _totalQuantity;
}

Hibernate Repository Class

I assume that you are already aware how to initialize/create/mapped nHibernate session.

To execute a stored procedure, hibernate uses the GetNamedQuery method.

public class HistoricalOrderSummaryDao 
{
    public List<HistoricalOrderSummary> GetCustomerOrderHistoryFor(string customerId) {
    IQuery query = NHibernateSession.GetNamedQuery("GetCustomerOrderHistory")
    query .SetString("CustomerID", customerId)
    query .SetResultTransformer(
    new NHibernate.Transform.AliasToBeanConstructorResultTransformer(
    typeof (HistoricalOrderSummary).GetConstructors()[0]));
    return query.List<HistoricalOrderSummary>() as List<HistoricalOrderSummary>;
}
private ISession NHibernateSession {
    get {
        return NHibernateSessionManager.Instance.GetSession();
        }
    }
}

Conclusion

It is not a difficult job to find the solution, but it is important to remember some tips. Please share your inputs or thoughts regarding the same. This article helped me a lot to complete my task. If you need any help or assistance, then please let me know. Thank you.

License

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

About the Author

ziaulh
Web Developer
India India
Member
want to contribute more on myself growth.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionuse stored proc in nhibernatememberswemme1810 May '12 - 17:58 
i do not have the hmb.xml file. I have put the tags in the hibernate.cfg.xml and won't work. I also put it inside the Client.nhv.xml but won't work. Where exactly can i put it so my code will work? thanks
 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <sql-query name="GetAllClient">
    <return-scalar column="FirstName" type="String" />
    <return-scalar column="ClientId" type="Int32" />
    exec GetAllClient
  </sql-query>
  <sql-query name="GetAllClient">
    <return class="Client" />
    exec dbo.GetAllClientyId
  </sql-query>
</hibernate-mapping>

Question[My vote of 2] SetResultTransformermemberabellix25 Aug '11 - 1:29 
The most interesting thing in your article is the Result Transformation, why don't you talk about it!
AnswerRe: [My vote of 2] SetResultTransformermemberziaulh27 Aug '11 - 22:55 
Hello abellix,
 
Thank you very much for your nice feedback.
 
This article is just a tip for how to use nhibernate.
 
I will upload complete article very soon.
 
Thank you once again.
 
Regards,
Ziaul
QuestionHow the column map to property work?membertola.chhoeun9 Oct '10 - 7:55 
In the mapping file (*.hbm.xml) there is a column name:
In the Object model there is a property name: public int TotalQuantity
 
I wonder how these two thing map together, or it is just a mistake?
I sorry I am new in NHibernate.
 
.Tola.
AnswerRe: How the column map to property work?memberziaulh18 Nov '10 - 18:23 
It is typo mistake...I will update the thread..
 
Thank you for your suggestion!!
GeneralSample CodememberSougandh29 Mar '10 - 20:04 
Hi There,
 
It would be great if you could include sample code for the same. I also wanted to know will this work without the class and table name definition.
 
Kindly revert back.
 
Regards,
Sougandh Pavithran

GeneralRe: Sample Codememberziaulh18 Nov '10 - 18:27 
Hello,
 
I am very much sorry to reply you late.
 
Here everything i have written as per hbm config file, Model object and Repository class.
 
you need to develop as per the sequence..
 
if you don't mind then share your code and still stuck then you can easily contact me any time.
 
Thank you,

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 20 Aug 2009
Article Copyright 2009 by ziaulh
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid