-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: evict cache is not working
PostPosted: Fri Apr 02, 2010 8:21 am 
Newbie

Joined: Tue Sep 18, 2007 11:24 am
Posts: 9
Hi,

On a project we are facing the following issue (Hibernate 3.5.0-Final). We are deleting entities using a HQL query (delete e from Entity e where ...).
Code:
Entity e = new Entity();
s.persist(e);


In a new transaction I do:
Code:
Entity e = s.get(Entity.class, id);
assertNotNull(e); //This is working
int nb = s.createQuery("delete e from Entity e").executeUpdate();
assertEquals(1, nb);
s.flush();
e = s.get(Entity.class, id);
assertNull(e); //This is NOT working as the entity is still in the cache


Of course Hibernate is not able to know what are the deleted entities so I would like to purge the cache manually.

I tried to use the evict method:
Code:
Entity e = s.get(Entity.class, id);
assertNotNull(e); //This is working
int nb = s.createQuery("delete e from Entity e").executeUpdate();
assertEquals(1, nb);
s.flush();
s.evict(Entity.class); // Try to purge the cache
e = s.get(Entity.class, id);
assertNull(e); //This is NOT working as the entity is still in the cache


So it seems the evict call is not taken into account. The only workaround I found is to replace the evict() call by clear() but clear will purge all entities and I would like to only purge Entity.class instances.

Do you know if this is a bug or if I did something wrong in the use of evict?

Thanks

Julien


Top
 Profile  
 
 Post subject: Re: evict cache is not working
PostPosted: Fri Apr 02, 2010 9:48 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi Julien,

session.evict(Object obj) expects a entity object as parameter, not a entity class.
You must call
Code:
s.evict(e);

instead to
Code:
s.evict(Entity.class);


then it works.


Top
 Profile  
 
 Post subject: Re: evict cache is not working
PostPosted: Fri Apr 02, 2010 10:19 am 
Newbie

Joined: Tue Sep 18, 2007 11:24 am
Posts: 9
Are you sure? Looking at the documentation: http://docs.jboss.org/hibernate/stable/ ... ssioncache I can use this syntax to delete all entity of the given type.

Code:
sessionFactory.evict(Cat.class);  //evict all Cats


Edit: In fact it seems I was not making a difference between session and 2nd level cache.

Thanks for the help!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.