This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Writing Update query in Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Writing Update query in Hibernate" Watch "Writing Update query in Hibernate" New topic

Writing Update query in Hibernate

chundottu jacob

Joined: Mar 13, 2006
Posts: 7

I need to write the following JDBC SQL using Hibernate.

String SQL = "Update MT_PERSON set DELETE_IND = 'Y' where "
+"((ACTIVE_ID=? and LINKED_ID=?) or (ACTIVE_ID=? "
+"and LINKED_ID=?)) and LINKED_TY = 'U'";

Can someone please help by providing me with a step-by-step procedure on how to do so using Hibernate.

Thanks in Advance.
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Well, I would suggest just using the SQL directly.


Change the "?" to use named bind variables, like :firstLinkedId

Then in your SQLQUery object set the parameters, then call executeQuery()?

I can't remember the exact method name, but an IDE will drop down with all the methods available to SQLQuery, and should be able to find the method.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
chundottu jacob

Joined: Mar 13, 2006
Posts: 7

Session.createSQLQuery(SQL) is not available in net.sf.hibernate.Session. I am using Hibernate version 2. Pleae advise how I could write the UPDATE query.

Thanks in advance.
Ghulam Rashid
Ranch Hand

Joined: Jan 14, 2002
Posts: 278
Sorry, I cant provide you the step-to-step solution. But, can share my idea.

Other approach would be apart from what discussed above.

First you can load the persistance class using session.find()/load() using the condition (ACTIVE_ID=? and LINKED_ID=?). And, the call the setter method and then session.saveOrUpdate.
I agree. Here's the link:
subject: Writing Update query in Hibernate
It's not a secret anymore!