-
Updating multiple records using Hibernate (6 messages)
- Posted by: Ashwin Khale
- Posted on: July 14 2006 11:47 EDT
Hi, I m new to hibernate. And I have a requirement that I need to update multiple records in a table using hibernate. Just as we write a simple SQL query - Update set = where . How do i perform the above task using hibernate. Thanks in advance.Threaded Messages (6)
- Re: Updating multiple records using Hibernate by Shahzad Masud on July 17 2006 08:16 EDT
- not entirely true by Benjamin Possolo on July 27 2006 06:48 EDT
-
Update code throws Exception by jack krish on July 11 2011 07:47 EDT
-
Updating multiple records using Hibernate by Japan Bhatt on July 12 2011 03:18 EDT
-
Update code throws Exception by jack krish on July 13 2011 06:01 EDT
- Updating multiple records using Hibernate by Japan Bhatt on July 17 2011 12:10 EDT
-
Update code throws Exception by jack krish on July 13 2011 06:01 EDT
-
Updating multiple records using Hibernate by Japan Bhatt on July 12 2011 03:18 EDT
-
Update code throws Exception by jack krish on July 11 2011 07:47 EDT
- not entirely true by Benjamin Possolo on July 27 2006 06:48 EDT
-
Re: Updating multiple records using Hibernate[ Go to top ]
- Posted by: Shahzad Masud
- Posted on: July 17 2006 08:16 EDT
- in response to Ashwin Khale
Hibernate (HQL) is more like Object to Entity mapping language. So there is not direct way of updating multiple rows with one statement. There is only one wayt of updating multiple rows; i.e. use native SQL to update the rows. -
not entirely true[ Go to top ]
- Posted by: Benjamin Possolo
- Posted on: July 27 2006 06:48 EDT
- in response to Shahzad Masud
in fact Hibernate does support DML Style operations. see section 13.4 of the Hibernate Reference manual for an explanation of performing such updates. example: Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); String hqlUpdate = "update Customer c set c.name = :newName where c.name = :oldName"; // or String hqlUpdate = "update Customer set name = :newName where name = :oldName"; int updatedEntities = s.createQuery( hqlUpdate ) .setString( "newName", newName ) .setString( "oldName", oldName ) .executeUpdate(); tx.commit(); session.close(); -
Update code throws Exception[ Go to top ]
- Posted by: jack krish
- Posted on: July 11 2011 07:47 EDT
- in response to Benjamin Possolo
Hi,i am new to this forum,
I given try to this code.but this is not wotking,i am getting the below problem
"org.hibernate.queryException : Query must be bign with Select or From ...." .
May be i am wrong. pls tell me how to solve this problem .i dont know why i am facing this problem.
i have tried in so many ways, still the problem is existing . now i got confused whether hibernate support update query or not . if yes, how can i do that ,
i want to update the table with where cluase . this is my requirement. pls help me to come out from this plbm.
Any early help would be appriciate...
Thanks in Advance
-
Updating multiple records using Hibernate[ Go to top ]
- Posted by: Japan Bhatt
- Posted on: July 12 2011 15:18 EDT
- in response to jack krish
try to get the persistant object from the DB and in the same Transaction update the value to the same object and finally close the transaction.
Hibernate will take care of updating the object to the DB
-
Update code throws Exception[ Go to top ]
- Posted by: jack krish
- Posted on: July 13 2011 06:01 EDT
- in response to Japan Bhatt
Sorry ,i can't get u clearly,
can u please explain some more ..
below is the code which i have used in my project ,
transaction=session.beginTransaction();
String hqlUpdateQuery="update postVO set actiontakenby=:actiontionTakenBy where internalID='10'
Query query=session.createQuery(hqlUpdateQuery);
query.setParameter("actionTakenBy",approverID);
//OR tried this one also
query.setString("actionTakenBy",approverID);
query.executeUpdate();
can you please tell me where i did the mistake . i think, hibernate not even consider the query ,while start reading the query itself its throwing error .
I did it in another way using session.get(classname.class,id); and setting the appropriate value in setter method .this is working fine, i am getting required result , but the pbm is seems to execute the query too many times (runing select query once and update query once ) and also not sure whether this is the feasible solution or not .
any one help me to solve this ....
Thanks in Advance ..
-
Updating multiple records using Hibernate[ Go to top ]
- Posted by: Japan Bhatt
- Posted on: July 17 2011 00:10 EDT
- in response to jack krish
yes exactly, this was what I was suggesting you.
Get the persistant object first using session.get() and in the same transaction update the object and then close the transaction. Hibernate will take care of updating the object for you.
and since you are updating the object in DB, you need to retrive it first and then update the object and finally flush it into DB. So for sure it will include select query and then update query.
If you try to update the object by preparing it in your java world itself and not getting the fresh one from DB, then it may create certain Synchronization probs. for you.