Hibernate 緩存_1
List blogs = sess.createQuery("from Blog blog where blog.blogger = :blogger")
.setEntity("blogger", blogger)
.setMaxResults(15)
.setCacheable(true)
.setCacheRegion("frontpages")
.list();
如果查詢需要強行刷新其查詢緩存區域,那么你應該調用Query.setCacheMode(CacheMode.REFRESH)方法。 這對在其他進程中修改底層數據(例如,不通過Hibernate修改數據),或對那些需要選擇性更新特定查詢結果集的情況特別有用。 這是對SessionFactory.evictQueries()的更為有效的替代方案,同樣可以清除查詢緩存區域。
Session還提供了一個contains()方法,用來判斷某個實例是否處于當前session的緩存中。
如若要把所有的對象從session緩存中徹底清除,則需要調用Session.clear()。
對于二級緩存來說,在SessionFactory中定義了許多方法, 清除緩存中實例、整個類、集合實例或者整個集合。
sessionFactory.evict(Cat.class, catId); //evict a particular Cat
sessionFactory.evict(Cat.class); //evict all Cats
sessionFactory.evictCollection("Cat.kittens", catId); //evict a particular collection of kittens
sessionFactory.evictCollection("Cat.kittens"); //evict all kitten collections