Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Evict clarification

 
sarma kiran
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sarma kiran
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic