Difference between save and persist method in Hibernate - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Friday, June 5, 2020

Difference between save and persist method in Hibernate

Both save and persist methods of Session class persist given transient instance into database.

  1. The first difference between these two methods are their return type. Return type of save method is Serializable object while return type of persist method is void.
  2. Both method persist given transient instance into database. However, persist method doest guarantee identifier value will be assigned to persistence instance immediately, the assignment might happen at flush time.
  3. save operation cascades to associated instances if the association is mapped with cascade="save-update". While persist operation cascades to associated instances if the association is mapped with cascade="persist".
  4. The save method persist a given transient instance inside and outside of a transaction boundary. While, The persist method does not execute a insert query if it is outside of a transaction boundary.
  5. The persist method is called outside of transaction boundaries, it is useful in long-running conversations with an extended Session context. On the other hand save method is not good in a long-running conversation with an extended Session context.
  6. The save method is only supported by Hibernate while persist method is supported by Hibernate and JPA too.

Example of save in Hibernate

package com.walking.techie.entity;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;

@Entity
@Table(name = "person")
@Getter
@Setter
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private String name;
}
package com.walking.techie;

import com.walking.techie.entity.Person;
import com.walking.techie.utils.HibernateUtil;
import java.io.Serializable;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class SaveClient {

  public static void main(String[] args) {
    // get session factory of an application
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    // Open a session
    Session session = sessionFactory.openSession();

    // Begin a unit of work and return the associated Transaction object.
    Transaction transaction = session.beginTransaction();
    Person person = new Person();
    person.setName("Santosh");
    Serializable id = session.save(person);
    System.out.println("Person saved in DB with return serialized id: " + id);

    // commit the transaction
    transaction.commit();
    // End the session by releasing the JDBC connection and cleaning up.
    session.close();
  }
}

Once you run this application. You will see on console something like below:

Hibernate: create table person (id bigint not null auto_increment, name varchar(255), primary key (id)) engine=InnoDB
Hibernate: insert into person (name) values (?)
22:45:26,649 TRACE BasicBinder:65 - binding parameter [1] as [VARCHAR] - [Santosh]
Person saved in DB with return serialized id: 1

Example of persist in Hibernate

We will use the same Person entity for this example also.

package com.walking.techie;

import com.walking.techie.entity.Person;
import com.walking.techie.utils.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class PersistClient {

  public static void main(String[] args) {
    // get session factory of an application
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    // Open a session
    Session session = sessionFactory.openSession();

    // Begin a unit of work and return the associated Transaction object.
    Transaction transaction = session.beginTransaction();
    Person person = new Person();
    person.setName("Mohan");
    session.persist(person);
    // commit the transaction
    transaction.commit();
    // End the session by releasing the JDBC connection and cleaning up.
    session.close();
  }
}
Hibernate: insert into person (name) values (?)
22:49:29,994 TRACE BasicBinder:65 - binding parameter [1] as [VARCHAR] - [Mohan]

After running the both examples. We have records like this in "person" table.

Record in person table

No comments :

Post a Comment