Pages Navigation Menu

Coding is much easier than you think

Hibernate update query example


 
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;

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

	@Id
	@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




   
   oracle.jdbc.driver.OracleDriver
   system
   admin
   jdbc:oracle:thin:@127.0.0.1:1521:XE

   
   org.hibernate.dialect.Oracle10gDialect

   
   true

   
   update
   
   




 

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();
		srb.applySettings(cf.getProperties());
		ServiceRegistry sr = srb.build();
		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"
		std.setName("Ameer");
		std.setCollege("PSG");
		tx.commit(); // Update method will be called implicitly.
		System.out.println("Object Updated successfully !");
		session.close();
		sf.close();
	}
}

 

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

 

 

Run it – Eclipse Console

 

 

Data in DB after running the program