HQL Update Statement to update database table

Update query in HQL is used to update rows in the given database tables. This article illustrates how to used the UPDATE statement in Hibernate.

HQL Update Statement to update database table

HQL Update Statement to update database table

HQL's update query statement is used to update the values of database rows. Though HQL is similar to SQL but instead of running on tables it directly works with Hiberante persistent objects and their properties. Hibernate query language (HQL) also supports WHERE, SELECT, INNER JOIN, GROUP BY Statements except UPDATE, INSERT and DELETE Statements to perform various useful operations on database tables.

In this article we are testing an example to update the values of database table. See the example below...

Example of UPDATE Clause in Hibernate:

package roseindia.tutorial.hibernate;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class UpdateExample {
  /**
 * @param args
 */
  public static void main(String[] args) {
  // TODO Auto-generated method stub
  Session sess = null;
  try {
  SessionFactory fact = new Configuration()
.configure().buildSessionFactory();
  sess = fact.openSession();
  Transaction tr = sess.beginTransaction();
  Insurance ins = (Insurance)sess.get
(Insurance.class, new Long(1));
  ins.setInsuranceName("Jivan Dhara");
  ins.setInvestementAmount(20000);
  ins.setInvestementDate(new Date());
  sess.update(ins);
  tr.commit();
  sess.close();
  System.out.println("Update successfully!");
  }
  catch(Exception e){
  System.out.println(e.getMessage());
  }
  }
}

Output of UPDATE Statement:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.

Hibernate: select insurance0_.ID as ID0_0_, insurance0_.insurance_name as insurance2_0_0_, insurance0_.invested_amount as invested3_0_0_, insurance0_.investement_date as investem4_0_0_ from insurance insurance0_ where insurance0_.ID=?

Hibernate: update insurance set insurance_name=?, invested_amount=?, investement_date=? where ID=?

Update successfully!

Tutorials

  1. Hibernate 6 New Features
  2. Hibernate 6 Maven Dependency
  3. Hibernate 6 Tutorial
  4. Hibernate Java - Hibernate Open Source ORM tool in Java
  5. What is difference between Hibernate 4 and 5?
  6. Hibernate 5.3 features
  7. Hibernate 5 JPA Configuration
  8. Hibernate 5 persistence.xml example
  9. Hibernate 5 JPA Tutorial
  10. Hibernate and JPA Example
  11. How properties of a class are mapped to the columns of a database table in Hibernate?
  12. Hibernate 5 Annotations Maven dependency
  13. Hibernate 5 annotationconfiguration
  14. Doctype Hibernate-configuration 5
  15. Hibernate 5 Dialect
  16. Hibernate 5 configuration dtd
  17. Hibernate 5 Hello World
  18. Getting started with Hibernate 5
  19. Hibernate 5.2.10 jar Download
  20. Hibernate 5.2.10 Maven Dependency
  21. Hibernate 5 Maven Dependency
  22. Hibernate 5 native query example
  23. Hibernate 5 query deprecated
  24. When should you use Hibernate?
  25. What's new in Hibernate 5?
  26. How much time it takes to learn Hibernate?
  27. Hibernate envers custom revinfo table
  28. Hibernate 5 Envers
  29. Hibernate Envers get all revisions
  30. Hibernate Examples
  31. Hibernate 5 build SessionFactory Example
  32. Hibernate 5 SessionFactory Example
  33. Hibernate 5 Annotation Example
  34. How to download Hibernate 5.x?
  35. Hibernate 5 Features
  36. Hibernate 5 Tutorials
  37. Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java
  38. How to write hello world example program in Hibernate?
  39. How to create one to one relationship in Hibernate 4 with Annotation?
  40. How to calculate minimum in Hibernate using the min() Function