• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in an Object instance while using evict() method

 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Session session=HibernateUtil.beginTransaction();
User user1 = new User();
user1.setPassword("aaaaaa");
Long id = (Long)session.save(user1);
session.evict(user1);
User user2 = (User)session.get(User.class, id);
user1.setVerified(true);
session.saveOrUpdate(user1);
HibernateUtil.commitTransaction();


Here i've not committed "user1" instance.
I've just cached "user1" instance using save method & as I know if we don't commit the transaction that instance would not have been saved to Database right.
By that rule, "user1" would not have been saved to Database & when I try to look for that instance using get method


User user2 = (User)session.get(User.class, id);


it should throw some exception right.
please throw some light on this?
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any reply?
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hibernate does not synchronize its session with the database only while committing a transaction.
It can do it when it "senses" it is necessary to keep a certain coherency.

In your piece of code hibernate actually saves your user1 to give you the id. After the evict() user1 is a detached object, therefore no more in the hibernate session.
So when you get user2, it is an object with the same idea than user1 but located in the session.
There is no conflict.
 
reply
    Bookmark Topic Watch Topic
  • New Topic