Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Hibernate Evict clarification

 
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
 
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
 
author and cow tipper
Posts: 5000
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
 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic