1:注入EntityManager
@PersistenceContext
private EntityManager entityManger;
2:获取hibernate缓存对象
CacheImpl cache=(CacheImpl)entityManger.getEntityManagerFactory().getCache();
3:接下来就可以随心所欲过期不需要的缓存,如实体缓存,集合缓存,查询缓存等,具体方法自己去查看
org.hibernate.internal.CacheImpl类,如清除全部类型缓存是cache.evictAllRegions()
主要有以下几类:
(1)evictCollectionRegions():集合缓存相关
(2)evictDefaultQueryRegion();查询缓存
(3)evictEntityRegions();实体缓存
(4)evictQueryRegions();查询缓存
(5)evictNaturalIdRegions();不知道什么缓存
ps:有想要了解具体hibernate缓存详情的请查看友链:
https://www.baeldung.com/hibernate-second-level-cache, https://blog.csdn.net/czp11210/article/details/51996217