Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate stored procedures insert scenario

 
Anthony Sykes
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I have an .xhtml page that collect Widget information from a given user. After the user enters the infomation, I have a stored procedure that will take the information and insert it into a Widget table using session.save. Now for the tricky part, when the user initially entered the information there was no database record associated with this Widget thus no primary key. After the proc performs the insert, I need to somehow associate the primary key with the current Widget object. Does hibernate provide a mechanism for this?
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your session.save() returns the id of the inserted record.
Use that id and associate with the widget.
 
Anthony Sykes
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will work even when using stored procedures?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a nice article on stored procedures with Hibernate. This might answer some questions:

Stored Procedures with Hibernate

The worst case scenario is that you simply get back the id of the inserted or queried record. Once you have that id, as was said before, just load that object using either a DAO or the Hibernate Session itself. Then use that loaded POJO and volley it back and forth between your data layer and your web layer.

-Cameron McKenzie

 
Anthony Sykes
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
will you still have access to the same internal hibernate features you do without using stored procedures?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it all depends on what your resultset returns to you.

I'm not overly experienced in this aspect, but I don't think your resultset is going to return to you any persistent objects. So, I would imagine that any persistent POJO you want would need to be loaded from the Hibernate Session, using the primary key that is contained in the resultSet being returned.

Feel free to correct me on this point, but it makes sense to me.

-Cameron
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic