Hibernate - Difference Between save and persist Method with an Example

In this post, we will discuss the difference between Session.save() and Session.persist() method in Hibernate.

Hibernate - Difference between Session.save() and Session.persist() method

  1. The return type of the Session.save() method is java.io.Serializable. It returns the newly generated identifier id value as a Serializable object. While return type of the Session.persist() method is void (does not return anything).
Here are the return types of these methods from the Java API doc.
Session.save() method:
Serializable org.hibernate.Session.save(Object object)
Session.persist() method:
void org.hibernate.Session.persist(Object object)
  1. The Session.save() method can be used inside or outside the transaction boundaries. While Session.persist() method can be used only within the boundary of the transaction.
Session.save() method example: In the below example, Session.save() method is called after the transaction.commit() but it executes the INSERT statement and it will insert the record into the database (this example demonstrated in a video tutorial, link given in the next section).
public void saveExample(Student student) {
    Transaction transaction = null;
    try (Session session = HibernateJavaConfig.getSessionfactory().openSession()) {

        // start the transaction
        transaction = session.beginTransaction();

        // commit transaction
        transaction.commit();

        // save student entity
        Serializable id = session.save(student);
        System.out.println("database table id -> " + id);

    } catch (Exception e) {
        if (transaction != null) {
            transaction.rollback();
        }
    }
}
Session.persist() method example: In the below example, Session.persist() method is called after the transaction.commit() but it does not execute the INSERT statement and it won't insert the record into the database (this example demonstrated in a video tutorial, link given in the next section).
public void persistExample(Student student) {
    Transaction transaction = null;

    // try with resource statement to auto close session object
    try (Session session = HibernateJavaConfig.getSessionfactory().openSession()) {

        // start the transaction
        transaction = session.beginTransaction();

        // commit transaction
        transaction.commit();

        // persist student entity
        session.persist(student);

    } catch (Exception e) {
        if (transaction != null) {
            transaction.rollback();
        }
    }
}

  1. Another difference between Session.persist() and Session.save() method is that both methods make a transient instance persistent. However, persist() method doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time.

Video

The below video explained the difference Session.save() and Session.persist() method in Hibernate with a coding example:

Related Posts

Comments