save(), saveOrUpdate(), persist() in Hibernate

Introduction

In this tutorial we will discuss about the differences among save(), saveOrUpdate() and persist() method in leading ORM(Object Relational Mapping) tool Hibernate framework.

The key thing is all these methods are used to store object(s) into database and they also make transient object(s) into persistent.

Hibernate framework’s Session class provides a few ways to save objects by methods save(), saveOrUpdate() and persist() based on requirements for persisting objects into database.

Method – save()

We will now look into the features of the save() method what it does:

  • Generates a new identifier and insert the record into databse
  • After inserting the new record it returns the Serializable object
  • Generates SQL query to insert the record
  • Makes transient instance persistent
  • Always gets executed and inserts the record and returns the Serializable object whether it is inside or outside of transaction boundaries
  • Is not useful for long running conversation with extended Session when it is inside transaction boundaries

Example

We create following example to save the entity into database. The save method returns the unique identifier for the inserted product details.

Transaction tx = session.beginTransaction();

Product product = new Product("Sample Product", "SP", 453.00);

Integer id = (Integer) session.save(product);

System.out.println("Id: " + id);

tx.commit();

It generates the following insert query:

insert 
into
	product
	(code, name, price) 
values
	(?, ?, ?)

The above code gives output: Id: 26

Method – saveOrUpdate()

This method has the following features:

  • Inserts or updates record depending upon the existence of the record in the database
  • Generates insert or update SQL statement depending upon the existence of the record in the database
  • Flexible to use but involves extra processing to find out the existence of the record
  • Does not return anything, i.e., return type of this method is void

Example

If you want to save the new record then do not set identifier value for the entity. It will save the entity and generate the identifier while inserting the product details.

Session session = HibernateUtils.getSessionFactory().getCurrentSession();

Transaction tx = session.beginTransaction();

Product product = new Product("Sample Product", "SP", 453.00);

session.saveOrUpdate(product);

tx.commit();

The above code generates the following insert query:

insert 
into
	product
	(code, name, price) 
values
	(?, ?, ?)
hibernate saveorupdate

If you want to update the existing record then use below code:

Look at the output the existing price has been updated. You will also notice in the console that update query has been generated instead of insert query.

update
        product 
    set
        code=?,
        name=?,
        price=? 
    where
        id=?
hibernate saveorupdate

Method – persist()

Method persist() has the following features:

  • Similar to save(), it also saves object into database but returns nothing, i.e., the return type of persist() is void
  • There is no guarantee that an identifier value to the persistent instance will be assigned immediately and it may happen during flush time

Example

The following code saves the entity into database.

Session session = HibernateUtils.getSessionFactory().getCurrentSession();

Transaction tx = session.beginTransaction();

Product product = new Product("Sample Product", "SP", 553.00);

session.persist(product);

tx.commit();

It also generates the following insert query similar to the save query:

insert 
into
	product
	(code, name, price) 
values
	(?, ?, ?)

Source Code

Download

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *