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.

Ads

Tutorials   
Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment
Ads

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!

Advertisements

Ads
Share on Google+Share on Google+

HQL Update Statement to update database table

Posted on: October 2, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: HQL Update Statement to update database table  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads