File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes native SQL update in Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "native SQL update in Hibernate" Watch "native SQL update in Hibernate" New topic

native SQL update in Hibernate

Deepika Mahajan

Joined: Dec 03, 2008
Posts: 3
I want to execute native SQL update query in Hibernate.

Can anyone tell me how to user <sql-update> tag while calling SQL update query in Hibernate?

Is it possible to use this tag in Hibernate2.0 or not?

Your help is highly appreciated.
Brett Maclean

Joined: May 01, 2009
Posts: 22
Hi Deepika

The <sql-update> element (along with the insert and delete) are for when you want your class-entity mapping to have DML that you define rather than Hibernate. So you will see these elements within a class mapping as per the code snippet at the top of this article.

Reading between the lines, I'm not sure that this is what you are after. It sounds as if you just want to issue an update statement for a non-managed entity in which case you're probably better off having the UPDATE statement in a stored procedure and calling that. There's documentation on the Hibernate site about how to call stored procedures.

If however, you are issuing updates for a managed entity (i.e. one that you map via a <class> definition) then you will need to be careful as it's Hibernate's Sessions that need to be looking after the entity really. In this case, the best option is to use the custom update element within the class's mapping. The Hibernate link above goes on to describe how to do this.
Follow us on Twitter:
Deepika Mahajan

Joined: Dec 03, 2008
Posts: 3
Thanks for the reply.

Actually my problem is, I want to execute a complex Update SQL query with nested Select queries in it, using hibernate. But As hibernate dosen't support Update with nested Select clauses to a level, I am trying to execute SQL query using <sql-query> tag not HQL query. But I got error saying - update operation is supported via HQL only.

Now I have split this update query to two parts, Select first and then Update. But here also i am stuck.

Can you tell me how to map a object returned from DB to our java bean? After excuting SELECT I am getting list of objects and these objects cannot be mapped to my model class/java bean.

Is there any way to do that.
I agree. Here's the link:
subject: native SQL update in Hibernate
It's not a secret anymore!