Discussions

General J2EE: Updating multiple records using Hibernate

  1. Updating multiple records using Hibernate (6 messages)

    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.
  2. 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.
  3. not entirely true[ Go to top ]

    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();
  4. Update code throws Exception[ Go to top ]

    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

     

     

     

     

     

  5. 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

  6. Update code throws Exception[ Go to top ]

    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 ..

     

     

  7. 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.