File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes evict() method doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "evict() method doubt" Watch "evict() method doubt" New topic

evict() method doubt

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243


Why would I need this method? Why would I clear off the cache using this method, in the following scenario that I got from the Hibernate Quickly book,

Event one = (Event) session.load (Event.class, eventId);
one.setName ("Culturals");

if (session.contains(one)) {
session.evict ();

Event two = new Event ();
two.setId (eventId);
two.saveOrUpdate ();

Is it for the reason that I simply want to associate the id with another instance, I use the evict () method?

SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17249

Yes, lots of reasons to use evict. If you load too many objects into the Session you can run out of memory. So if you end up doing any long running sessions, you might want to evict out objects to save memory. It is a method that allows you full control over the cache's. There is also an evict() method in the Factory class to remove objects from the second level cache, if you are using second level cache.

Hope that helps clarify things for you.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243

Thanks Mark. Totally understood!
jQuery in Action, 2nd edition
subject: evict() method doubt
Similar Threads
Id not availble in lazy loaded associate objects
is it possible to evict cascade a cleared collection?
Hibernate: session.get()
Hibernate: one-to-many bidirectional mapping. Stack Overflow
Splitting an XML file