Recently I ran into a situation with NHibernate where I needed to call a stored procedure. Ok. That should be easy right? Well, not so much. I mean it wasn't rocket science, but it didn't feel like a first class citizen in the NHibernate framework, and maybe it's not supposed to be...I don't know.
So I googled it, of course, and I found a few related blog posts referencing the different ways of going about this. The problems started when my stored procedure needed to return multiple rows and not a scalar value. By problem I mean I had to do another Google search. :)
I found the following solution:
public IList<IPityTheFoo> FindByFoo(string foo) { return HibernateTemplate.SessionFactory.GetCurrentSession() .GetNamedQuery("StoredProcedureName") .SetParameter("Foo", foo) .List<IPityTheFoo>(); }
The general idea here is that you need to call GetNamedQuery which expects the name of the query as defined by your Hibernate Mapping File:
<sql-query name="StoredProcedureName"> <return class="IPityTheFoo"> <return-property name="Foo" column="Foo"/> </return> exec [dbo].[StoredProcedureName] ? </sql-query>
The Spring.NET HibernateTemplate exposes the current session through the SessionFactory.GetCurrentSession(). To return the collection of your objects simpoly call List<T>().
Related posts
Comments
May 7. 2009 09:21
Some solutions have been documented but I ran into other issues and ended up the solution a little further. Long story short, I ended up creating a custom Pipeline Component to strip out DTD’s and add Namespaces using regular expressions. At first I created a cXML cleaner component that worked specifically for my scenario. This worked fine but eventually I noticed that I was using a regular expression to find and replace the DTD which led me to think “Why can’t I use any regex I pass in and replace anything I want to?” So I did. I even made it open source so no one ever has to feel the anguish of having to develop custom BizTalk Pipeline Components, because it sucks. Bad.
saç ekimi
July 1. 2009 16:53
nice blog.
vajinismus
Home
© Rapidparts, Inc 2008 | 2950 Walkent Ct. NW, Grand Rapids, MI 49544 | Phone 616.647.2500 | info@rpionline.com