April 30 2015

Difference between save and persist in hibernate

 Difference between save and persist in hibernate       

The save and persist methods are used to persist the transient instance. Save and persist method results a sql insert query. If any properties are updated after the save or persist method and before the transaction is committed or session is flushed, the changes will be saved to the database using update query.

public Serializable save(Object object) throws HibernateException

public void persist(Object object) throws HibernateException

Difference between save and persist methods

  1. The return type of the save method is java.io.Serializable it returns the generated identity value whereas the return type of persist method is void it does not return anything.
  2. Persist method can be used only within the boundary of a transaction whereas save method can be used inside or outside the transaction boundaries
  3. Save method assigns the generated identifier immediately whereas persist method does not guarantee the assignment immediately it may assign at flush time.
  4. Save method take comparatively more time to execute than persist method.

Example

Student.java


package com.answersz.dao;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "student")
public class Student {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "STUDENT_ID")
private Integer studentId;

@Column(name = "STUDENT_NAME")
private String studentName;

@Column(name = "FATHER_NAME")
private String fatherName;

@Column(name = "DATE_OF_BIRTH")
private Date dateofBirth;

@Column(name = "COURSE")
private String course;

// getters and setters

}

Save Example

package com.answersz.dao;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

// Difference between save and persist in hibernate
public class SaveStudent {

 public static void main(String[] args) {

 SessionFactory factory = new AnnotationConfiguration().configure()
 .buildSessionFactory();

 Session session = factory.openSession();

 Transaction t = session.beginTransaction();

 Student s1 = new Student();
 s1.setStudentName("HARRY");
 s1.setFatherName("CHARLIE");
 s1.setDateofBirth(new Date());
 s1.setCourse("MTECH");
 Integer studId = (Integer) session.save(s1);
 System.out.println("Student Record Saved. Student Id : " + studId);
 System.out.println(studId);
 s1.setCourse("MS"); // update query
 t.commit();

 session.close();

 }

}

 

Output

Hibernate: insert into student (COURSE, DATE_OF_BIRTH, FATHER_NAME, STUDENT_NAME) values (?, ?, ?, ?)
Student Record Saved. Student Id : 940
940
Hibernate: update student set COURSE=?, DATE_OF_BIRTH=?, FATHER_NAME=?, STUDENT_NAME=? where STUDENT_ID=?

Persist Example

package com.answersz.dao;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

// Difference between save and persist in hibernate
public class PersistStudent {

 public static void main(String[] args) {

 SessionFactory factory = new AnnotationConfiguration().configure()
 .buildSessionFactory();

 Session session = factory.openSession();

 Transaction t = session.beginTransaction();

 Student s1 = new Student();
 s1.setStudentName("JOSEPH");
 s1.setFatherName("GEORGE");
 s1.setDateofBirth(new Date());
 s1.setCourse("MS");
 session.persist(s1);
 System.out.println("Student Record Saved. Student ID : "
 + s1.getStudentId());
 s1.setCourse("MTECH"); // Update Query
 t.commit();

 session.close();

 }

}

Output

Hibernate: insert into student (COURSE, DATE_OF_BIRTH, FATHER_NAME, STUDENT_NAME) values (?, ?, ?, ?)
Student Record Saved. Student ID : 941
Hibernate: update student set COURSE=?, DATE_OF_BIRTH=?, FATHER_NAME=?, STUDENT_NAME=? where STUDENT_ID=?

Table

select * from student;

STUDENT_ID STUDENT_NAME FATHER_NAME DATE_OF_BIRTH COURSE
940 HARRY CHARLIE 4/30/2015 MS
941 JOSEPH GEORGE 4/30/2015 MTECH

Related Articles


Copyright © 2014. All rights reserved.

Posted April 30, 2015 by answersz in category Hibernate

Leave a Comment

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

*