Home  >  Hibernate

Hibernate Session: evict() and merge() Example

By Arvind Rai, February 18, 2015
This page will provide hibernate session evict() and merge() example. In our hibernate development, sometimes we need to detach the session object because we need to change it and it may take time while changing. An easy example is if we are waiting for user input, in this case it is better to detach object from hibernate session, we do it by evict() method. After changing, we need to attach it in hibernate session. To attach, we use merge() object.

evict()

To detach the object from session cache, hibernate provides evict() method. After detaching the object from the session, any change to object will not be persisted. The associated objects will also be detached if the association is mapped with cascade="evict".

evict(Object object): Accept the object which is already synched with session.

merge()

merge() method is used to merge an object with persistent object on the basis of same identifier. The object as an argument is not changed and method returns persistent object. Find the below points to understand it. Associated objects are also merged if cascade type is as cascade="merge".

1. The state of object passed as an argument is copied to the object in the hibernate session with same identifier and return the persistent object.

2. If the object is not already present in the session with the same identifier as passed in the argument, first the session loads the object for the identifier of object passed as an argument then merge it and return the persistent object of the session.

3. If the persistent object for the identifier of object passed in argument, is not already in session and database then a copy of object passed as an argument is persisted and is returned.

merge(Object object) : Accepts the entity object and returns persistent object.
merge(String entityName, Object object) : Pass entity name and entity object.

Now find the sample example.
EvictAndMerge.java
package com.concretepage;
import org.hibernate.Session;
public class EvictAndMerge {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Student ss = (Student)session.get(Student.class, new Integer(3));
		System.out.println("Get Age from Database:"+ ss.getAge());
    	        //evict the object
		session.evict(ss);
		ss.setAge(25);
		session.flush();
		ss = (Student)session.get(Student.class, new Integer(3));
		System.out.println("Age after evict:"+ ss.getAge());
		//merge the object
		session.merge(ss);
		ss.setAge(30);
		session.flush();
		ss = (Student)session.get(Student.class, new Integer(3));
		System.out.println("Age after merge:"+ ss.getAge());
		session.close();
	}
} 
Find the output.
Hibernate: select student0_.id as id1_0_0_, student0_.age as age2_0_0_, student0_.name as name3_0_0_ from student student0_ where student0_.id=?
Get Age from Database:22
Hibernate: select student0_.id as id1_0_0_, student0_.age as age2_0_0_, student0_.name as name3_0_0_ from student student0_ where student0_.id=?
Age after evict:22
Hibernate: update student set age=?, name=? where id=?
Age after merge:30 

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS



Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us