-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Can I use stored procedures in NHibernate?
PostPosted: Wed Jun 22, 2005 8:39 am 
Newbie

Joined: Tue Jun 14, 2005 9:23 am
Posts: 9
.. hm..

my question in the subject.... ))


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 22, 2005 3:37 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
No, NH-139 is the JIRA issue for it, in case you want to track it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 6:26 am 
Newbie

Joined: Tue Jun 14, 2005 9:23 am
Posts: 9
This is very ruefully :((

I have a very complicated logic in a stored procedure. Maybe there is some way that allows me to use it in NHibernate? Or I need to wait?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 7:14 am 
Beginner
Beginner

Joined: Thu May 12, 2005 3:41 am
Posts: 24
Location: London, UK
You can obtain an IDbConnection object from the NHibernate session, and execute any commands using this. The following code assumes that the session object is an open NH session, and runs stored procedure MyStoredProc.
Code:
IDbConnection cn = session.Connection;
IDbCommand cmd = cn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "MyStoredProc";
cmd.ExecuteNonQuery();

What you can't currently do is run a stored procedure that returns data and map the data to persistent objects.

You also need to be aware that if your stored procedure amends the data in your tables, this will not be reflected in any persistent objects already loaded.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 7:37 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
dfed wrote:
I have a very complicated logic in a stored procedure. Maybe there is some way that allows me to use it in NHibernate? Or I need to wait?


Finally, you should consider moving (as much as possible) your logic from stored procs to your entities :wink:

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 7:53 am 
Newbie

Joined: Tue Jun 14, 2005 9:23 am
Posts: 9
That is bad, because the procedure returns data...and I need it.

How I understand, there is no the way at this moment in NHibernate to load data from stored procedure to persistent objects :(


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 8:06 am 
Newbie

Joined: Tue Jun 14, 2005 9:23 am
Posts: 9
2KPixel

This is mpossible or is very difficult...


Top
 Profile  
 
 Post subject: Use IClassPersister
PostPosted: Fri Jun 24, 2005 6:10 pm 
Newbie

Joined: Wed Jun 22, 2005 4:20 pm
Posts: 3
Location: San Jose, CA - USA
Regarding using stored procedures:

1) For saving (inserting/updating/deleting): Because a class can define its own persistence strategy (by using its own IClassPersister implementation and using the attribute "persister" in the mapping file), it is possible to redirect the insert/update/delete operations to call stored procedures, instead of generating dynamic SQL.

In this case, the basic strategy would be:

o Inherit from the appropriate IClassPersister class (AbstractEntityPersister, EntitiyPersister or NormalizedEntityPersister)

o Override the corresponding Generate[Insert | Update | Delete]Strings method, to build a stored procedure call, instead of a DML SQL string.

o Override the corresponding action method(s) (Insert(), Delete(), Update()).

2) For loading data: I guess that if you are using a RDMBS that supports user-defined functions that return result sets, the best strategy is to:

o Move the logic from the stored procedure to a user-defined function
o Create a view that returns the result set from the user-defined function
o Use the columns of the view in the Xml mapping file
o Of course, to persist such class, you will need to define you own IClassPersister, as explained in the item 1.

I have tested item 1 with my object model and worked fine. Item 2 still needs to be tested.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.