[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of Flex 4 in Action this week in the Flex forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Object Relational Mapping
 
RSS feed
 
New topic
Author

Hibernate Evict clarification

sarma kiran
Greenhorn

Joined: Jul 17, 2008
Messages: 28

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
Messages: 63

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
sarma kiran
Greenhorn

Joined: Jul 17, 2008
Messages: 28

Thanks
Cameron Wallace McKenzie
author and cow tipper
Bartender

Joined: Aug 26, 2006
Messages: 4594

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???, Portlet Programming Made Easy and the SCJA Certification Guides
My Hibernate and JPA Tutorials * My Mock Java Certification Exams * My Online Java Tutorials * My CBT Portlet Tutorials.

 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Object Relational Mapping
 
RSS feed
 
New topic
MyEclipse Enterprise Workbench