Share on Google+Share on Google+

Hibernate update Method

In this tutorial you will learn about the update method in Hibernate

Hibernate update Method

In this tutorial you will learn about the update method in Hibernate

Hibernate's update method saves the modified value into the table after the modification made. Using this method you can update the modified object with the help of  an identifier. This method throws an exception if an identifier is not existed or missed.

Example :

Here I am giving a simple example which will demonstrate you how an update method is used in Hibernate. For this I am giving a complete source code which can be downloaded. To achieve the solution of our problem at first I have created a table named employee with the fields Id, and Name and then created a POJO / Persistent class named to fetch the persistent object, a hibernate.cfg.xml file that the Hibernate can utilize it to create connection pool and the required environment setup, an employee.hbm.xml file to map an Employee object with table employee. And finally create a simple java file using which we will use an update method of Hibernate to update the record of table.

Complete Code :

package roseindia;

public class Employee {
private int id;
private String name;

public Employee()

public Employee(String name)
{ = name;
public int getId() {
return id;
public void setId(int id) { = id;
public String getName() {
return name;
public void setName(String name) { = name;


<?xml version='1.0'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<hibernate-mapping package="roseindia">
<class name="Employee" table="employee">
<id name="id" type="int" column="Id" >
<generator class="assigned"/>
<property name="name">
<column name="Name" />


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>


package roseindia;

import java.util.Scanner;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class EmpUpdate {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

public static void main(String args[]) {

Session session =null;


Configuration cfg = new Configuration().addResource("roseindia/employee.hbm.xml").configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
sessionFactory = cfg.buildSessionFactory(serviceRegistry);
catch(Throwable th)
System.err.println("Failed to create sessionFactory object."+ th);
throw new ExceptionInInitializerError(th);


session = sessionFactory.openSession();
Employee employee = new Employee();
Scanner scan = new Scanner(;
Transaction tr = session.beginTransaction();
System.out.println("Enter EmpId No. : ");
int id = scan.nextInt();
System.out.println("Enter the EmpName to replace : ");
String replace =;
System.out.println("Successfully updated");
catch(Exception e)

Output :

1. Table before modification

2. When you will execute the java file and provide the data as per instruction look at on console you will get the output as :

3. Table after modification : you will see the data of row 2 & column 2 is changed to vinay from india.

Download Source Code


Posted on: February 24, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Hibernate update Method  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
rajesh kumar
August 8, 2012
hibernate and struts

i have a table with 9 fields and 2 fileld primary key(num_group_code,dt_change) (1):-first time data insert (2):second time I edit then same primary key on value second row in insert ,and with dt_effect_F to this value change to dt_effect_T in first row and isValid change 1 to 0 and second row isValid 1. CREATE TABLE AMIT.UMS_MST_GROUP ( NUM_GROUP_CODE NUMBER(2) primary key NOT NULL, STR_GROUP_NAME VARCHAR2(20 BYTE) NOT NULL, NUM_DESIG_CODE NUMBER(3) NOT NULL, DT_EFFECT_F DATE NOT NULL, DT_EFFECT_T DATE, IS_VALID NUMBER(1) DEFAULT 1 NOT NULL, NUM_CHANGEDBY NUMBER(5) NOT NULL, DT_CHANGE DATE primary key DEFAULT sysdate NOT NULL, IS_LOCK NUMBER(1) DEFAULT 0 NOT NULL )
October 3, 2012
How can update only one attribute

Hi! This is an Excelent tutorial. How can I do, if I need update only one attribute and keep the other values from attributes. regards