Pages Navigation Menu

Coding is much easier than you think

Hibernate update query example

Update Query
In previous article we learnt to implement Hibernate delete Query example in Eclipse, in this article I will implement a program to update an object which is already persisted in the database.

Project Structure


Entity class


package entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "STUDENT")
public class StudentEntity {

	@Column(name = "ID")
	private int id;
	@Column(name = "NAME")
	private String name;
	@Column(name = "DEPARTMENT")
	private String department;
	@Column(name = "COLLEGE")
	private String college;

// Create Getters and Setters

Note: I have explained about every annotations used in the above file in the article Generic Hibernate Application Requirements

Hibernate Configuration file

File: hibernate.cfg.xml


   <!-- Database connection settings -->
   <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
   <property name="hibernate.connection.username">system</property>
   <property name="hibernate.connection.password">admin</property>
   <property name="hibernate.connection.url">jdbc:oracle:thin:@</property>

   <!-- SQL dialect -->
   <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>

   <!-- Echo all executed SQL to sysout -->
   <property name="show_sql">true</property>

   <!-- Create/Update the database schema on startup -->
   <property name="">update</property>
   <!-- Mapping file -->
   <mapping resource="Student.hbm.xml" />




Hibernate Utility


package util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import model.Student;

public class HibernateUtil {

	public static void main(String[] args) {

		Configuration cf = new Configuration().configure("hibernate.cfg.xml");

		StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder();
		ServiceRegistry sr =;
		SessionFactory sf = cf.buildSessionFactory(sr);

		Session session = sf.openSession();

		Student std = (Student) session.load(Student.class, new Integer(2));
		Transaction tx = session.beginTransaction();

		// std.setId(3); // We Should not update "id"
		tx.commit(); // Update method will be called implicitly.
		System.out.println("Object Updated successfully !");


Hibernate Cache

  • Whenever an object is loaded from database, hibernate stores the loaded object in cache memory which works in session scope.
  • Now if we do any modifications to the loaded object, then these modification are stored only in the object maintained by cache memory. Even if we modify the loaded object for multiple times then also the modifications will be stored only in the cached object.
  • But once we call transactions commit() method then hibernate will check whether there are any changes between the object present in the database and the object stored in the cache, now the if changes exists then hibernate will automatically call its update method internally and updates the student object in database.


Program Explanation

  • Line no 28: We should not update id, because we have loaded the object from the database only using this id member variable (See line no 25). So if we update this field, then hibernate will throws NonUniqueObjectException.
  • At line no 29, I have updated name property of student object.
  • Line no 31: So once commit() method is triggered from program, then hibernate will automatically call update method internally and updates the student object.


Data in DB before running the program

Hibernate DB

Run it – Eclipse Console

Hibernate Update

Data in DB after running the program

Update DB

About Mohaideen Jamil

Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.