Hibernate二级缓存设置

1、首先设置EhCache,建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下:



2、在Hibernate配置文件中设置:

     说明一下:如果不设置查询缓存,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()list()Iterator()createCriteria()createQuery()等方法获得的数据结果集的话, 就需要设置
hibernate.cache.use_query_cache true
才行

3、在Hbm文件中添加<cache usage=”read-only”/

4、如果需要查询缓存,还需要在使用QueryCriteria()时设置其setCacheable(true);属性

5、实践出真知,给一段测试程序,如果成功的话第二次查询时不会读取数据库