1

I am working on JSF application in which i am using Hibernate as ORM mapping tool.

Problem : I am using MySQL as database , so i am adding the data's into Mysql through hibernate.

Now i am trying to update a database value from bean class

When i trying to update it the query will execute successfully but the values updated values will not added in database

My Bean class Code :

 Session session=HibernateUtil.getSessionFactory().openSession();
 Query hQuery=session.createQuery("update Record set status='Present' where refId=100");
 System.out.println("Result : "+hQuery.executeUpdate());

The above code is to update the database values from table "record", Its showing no error in output but values are not updated in database.

 Hibernate : update sample.record set Status='Present' where RefId=100
 Result    : 9

In above result displayed in console, for showSql

Any suggestions would be really appreciated...

kark
  • 4,763
  • 6
  • 30
  • 44

2 Answers2

5

I did a mistake which is in committing hibernate transaction...

Updated Bean Class

      Transaction tx=null;
      Session hSession=HibernateUtil.getSessionFactory().openSession();
      Query hQuery=hSession.createQuery("update Leaverecord set status='HR' where refId="+lrb.getRefId());
      System.out.println("Result : "+hQuery.executeUpdate());
      tx=hSession.beginTransaction();
      tx.commit();
        hSession.close();

Output

Hibernate: update sample.record set Status='Present' where RefId=100
Result : 9

The mistake i did that i have not used the transaction in hibernate Update query, i thought that transaction query only for Hibernate insertion.

Now it is working fine...

kark
  • 4,763
  • 6
  • 30
  • 44
1

have you make that method transactional? ex: adding @Transactional annotations if you using springframework

David
  • 698
  • 1
  • 7
  • 12
  • 1
    are you sure that transaction is committed to database? check [link](http://stackoverflow.com/questions/14622962/what-is-transaction-commit-in-hibernate) .. – David Oct 11 '13 at 10:03