Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Update in Hibernate2

  Asked By: Kristopher    Date: Nov 19    Category: Java    Views: 1421

I have two problems about update in hibernate2,
1- How can I update without any load an object in Hibernate2?
2- Is there any way for batch update in Hibernate2?



5 Answers Found

Answer #1    Answered By: Samuel Costa     Answered On: Nov 19

For your first question, you can create/write a query directly in HQL or even native SQL that updates a field in some table. HQL is the preferred method usually but it requires that you learn yet another query language! SQL of course, should be slightly more familiar to you, but then it might tie you to a specific database.

Second question, yes. You'll need to configure the JDBC batch  size as a Hibernate property and control the session's first level cache by using the flush() and clear() methods on the session object  once you hit the batch size in a loop or something.

Answer #2    Answered By: Dirck Jansen     Answered On: Nov 19

I searched alot but I can't find any sample code for HQL update.
would u mind snding a sample code for hql update  please?

Answer #3    Answered By: Calais Bernard     Answered On: Nov 19

Take a look here: docs.jboss.org/.../batch.html#batch-direct
Scroll down directly to the "13.4. DML-style operations" section.

Hope this helps :)

The original authors of Hibernate actually have a famous book out called "Hibernate in Action" for which I think a 2nd edition came out recently. I dont recall if the book specifically references HQL but that might also be a good resource for you. Check it out...

Answer #4    Answered By: Calvin Banks     Answered On: Nov 19

yes, I have seen this link before but unfortunately this is about Hibernate3 not 2.
I cant find any thing about Update in Hibernate2.

Answer #5    Answered By: Ralph Murray     Answered On: Nov 19

Sorry! I must have missed that. After looking around carefully, it doesn’t seem like Hibernate 2 supports HQL updates. It might be a good time to upgrade now and save yourself all the trouble :)

But, you still have a number of options:

- You can update  an object  completely detached. The only catch here is, it must have its “id” field initialized and you must know that beforehand. This is particularly useful if you want to load  the object in one session and save it in another. Like I said, this requires that you populate the “id” field at some point to fool hibernate in thinking that the object was loaded sometime ago, or let hibernate actually load it and you save it in a different session. Both can be tricky and are hacks around the problem. I wouldn’t do this if I were you. You dont want thing to be loaded anyway, so the 2nd option is out regardless...

- The other way is, you can write native sql queries. Use the createSQLQuery() method of the session to write the query in a dialect you want and execute it later using the returned Query object. There’s no good example of this scenario out there, so you’re just gonna have to try. It’s a hit-and-miss process!

This is the link to Hibernate 2.1.3’s reference docs:


Take a look at sections “Updating Objects” and “Native SQL queries”.

Here’s also a link to the Query interface which provides the executeUpdate() method. The doc don’t mentions if the method is available for Hibernate 2 and I don’t have one available right now; check it out.


Didn't find what you were looking for? Find more on Update in Hibernate2 Or get search suggestion and latest updates.