The moose likes Object Relational Mapping and the fly likes Hibernate Evict clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Hibernate Evict clarification" Watch "Hibernate Evict clarification" New topic
Author

Hibernate Evict clarification

sarma kiran
Ranch Hand

Joined: Jul 17, 2008
Posts: 30
Hi,
I am new to this Hibernate technology. I am going through Hibernate in Action to learn this technology.
I have few doubts of using Evict method.

As of my understanding when we use the evict method the object is detached from the session.
After that when we update the detached object in the same session is it will be persisted or not.

This is the code which i am trying..

EmpDAO empDao = new EmpDAO();
Session sessionObj = empDao.getSession();
Transaction tx = sessionObj.getTransaction();

Emp emp = new Emp();
emp.setFirstname("Rama");
emp.setLastname("Krishna");
emp.setAddress("Blr");
emp.setSalary(200);

tx.begin();
sessionObj.save(emp);
tx.commit();
System.out.println("*** Transaction one completed");

sessionObj.evict(emp);

emp.setFirstname("Krishna");
emp.setAddress("rjy");

//sessionObj.saveOrUpdate(emp);
sessionObj.flush();

tx.begin();
sessionObj.update(emp);
tx.commit();

printEmployee("Employee Added Successfully!", emp);

sessionObj.close();


After execurting this code, first one is inserting into the database after that its updating the same row.

I am confused, How it is updating the row on the detached object..

can any one clarify this doubt..

Regards,
kiran
Lalit Bhatt
Ranch Hand

Joined: Dec 27, 2007
Posts: 69
When you say evict, the object is detached that means it is not monitored for dirty checking. However when you call update it's again become managed so any changes will go to the database. For further clarification see "Hibernate Persistent Context and Session" at here


Java-JavaEE Hibernate Spring Spring Roo Web Applications
sarma kiran
Ranch Hand

Joined: Jul 17, 2008
Posts: 30
Thanks
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4925

Indeed, while you may evict your POJO from the Hibernate session, a detached instance can once again become a 'persistent' instance, managed by Hibernate, as soon as it 'touches' the Hibernate Session, which can happen with a save, update, delete, query, etc. So, you detach your object, but it becomes persistent with the update.

-Cameron McKenzie


Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
 
 
subject: Hibernate Evict clarification
 
developer file tools