Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clear hibernate cache of specific domain class

Imagine i have following class:

class Test {
   String name    
   static mapping = {
      cache true
      version false
}

The goal would be to insert rows using native sql on the database level so hibernate wouldn't recognize those changes. how can i notifiy hibnerate about this new rows?

Is there something like -> Test.clearCache?

like image 904
hitty5 Avatar asked Jul 08 '11 13:07

hitty5


2 Answers

This answer is a little bit old... the evict method is deprecated... you can use

sessionFactory.cache.evictEntityRegion(Class.name)

this worked for me... just google the class and you will find all methods...

like image 131
Gil Avatar answered Oct 02 '22 15:10

Gil


Though it's a deprecated way, it's shorter and still works.

def sessionFactory // inject Hibernate sessionFactory
sessionFactory.evict(clazz, id)
// or evict all class instances:
sessionFactory.evict(clazz)

Documentation is here, see it for the up-to-date way with Cache.

like image 25
Victor Sergienko Avatar answered Oct 02 '22 13:10

Victor Sergienko