hibernate update query example

By | 2018-03-19T09:30:24+00:00 October 10th, 2015|Hibernate|0 Comments

In the previous tutorials we have discussed about complete CRUD operations using hibernate framework. In this tutorials we are going to discuss about what are the different ways to update an object using hibernate. And we can also conclude that which method is better than all the approaches.

Hibernate Update :

We can update an object in hibernate by calling the update() method, provided by the org.hibernate.Session. Though the update() method is used to update an object, there are two different ways to use update() method.

  • With out loading an object from database
  • Loading an object from database

Hibernate Update with out loading an object :

Usually if we want to update a record in database, first of all we should get that record and manipulate and save it to the database. it is a usual practice of updating the record in database.

But in hibernate if we really know the record id (primary key) we can directly update the record in database with out load or getting the record from database.

If we know the record id, then we can directly create a new object and assign the primary key to it and save using update() method. 

Example : In this example I am going to update the below 111  student object in database.

hibernate update example

Create Student.java


package com.onlinetutorialspoint.pojo;

public class Student implements java.io.Serializable {
package com.onlinetutorialspoint.pojo;

public class Student implements java.io.Serializable {

    private Integer id;
    private String name;
    private Integer rollNumber;

    public Student() {
    }

    public Student(Integer id, String name, Integer rollNumber) {
        this.id = id;
        this.name = name;
        this.rollNumber = rollNumber;
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getRollNumber() {
        return this.rollNumber;
    }

    public void setRollNumber(Integer rollNumber) {
        this.rollNumber = rollNumber;
    }

}

Student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class catalog="onlinetutorialspoint" name="com.onlinetutorialspoint.pojo.Student" table="student">
        <id name="id" type="java.lang.Integer">
            <column name="id"/>
        </id>
        <property name="name" type="string">
            <column length="50" name="name"/>
        </property>
        <property name="rollNumber" type="java.lang.Integer">
            <column name="rollnumber"/>
        </property>
    </class>
</hibernate-mapping>

Main class : HibernteUpdateDemo.java


package com.onlinetutorialspoint.service;

import com.onlinetutorialspoint.pojo.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernteUpdateDemo {

    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");
        SessionFactory factory = configuration.buildSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();

        Student student = new Student();
        student.setId(111);
        student.setName("chandra shekhar");
        student.setRollNumber(8469);
        session.update(student);
        tx.commit();
        session.close();

    }
}

Output:

INFO: Envers integration enabled? : true 
Hibernate: update onlinetutorialspoint.student set name=?, rollnumber=? where id=?

Database : hibente update example 2 By running the above application, we can update the student details using hibernate update method. Based on the Id property (primary key) hibernate will update the details in database. we can find the updated details on the above database screen shot.

Problem with This approach :

A problem with this approach is, we must set all the properties of an object. If we don’t set any one of the property then its default values (null) will be updated in the database.


Session session = factory.openSession();

Transaction tx = session.beginTransaction();

Student student = new Student();
student.setId(111);
student.setRollNumber(8469);
session.update(student);
tx.commit();
session.close();

On the above example, we didn’t set studentName property then null will be updated in the name column in the database.

Hibernate Update with loading an object :

By using this approach, we can update an object with loading the object from the database. We can load the object from database by calling load() or get() methods. There are some functionality differences between the load() vs get() methods in hibernate. Example :


package com.onlinetutorialspoint.service;

import com.onlinetutorialspoint.pojo.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class UpdateDemo {

    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");
        SessionFactory factory = configuration.buildSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        Student student = session.load(Student.class, 111);
        student.setName("Johnson");
        session.update(student);
        tx.commit();
        session.close();

    }
}

Output :

INFO: Envers integration enabled? : true 
Hibernate: select student0_.id as id1_0_0_, student0_.name as name2_0_0_, student0_.rollnumber as rollnumb3_0_0_ from onlinetutorialspoint.student student0_ where student0_.id=? Hibernate: update onlinetutorialspoint.student set name=?, rollnumber=? where id=?

Database : Hibernate update DatabaseOn the above example, we got an object of student by calling load() method. On that object we can make the changes and update by calling update() method. But for this approach we no need to set all the properties of a student. We can only set what ever we want to update, the other properties will not effected in the database.

From the above two approaches, second one (Hibernate update with loading an object) is recommended approach, because here we no need to set all the properties. Based on our requirement we can update the values as we want.

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment