昨天下载了一个hibernate4以上的版本,合计使用EhCache二级缓存,但是就是报上面的异常,找了好多资料还是不知道找到答案,最后将hibernate的缓存3以上的版本,二级缓存生效了,可以使用了,看来hibernate4和3改变了好多东西啊。
在hibernate3下面使用二级缓存的实例
设置使用二级缓存
方式1在hibernate.cfg文件中添加
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
说使用Configuration配置
cfg.setProperty("hibernate.cache.provider_class","org.hibernate.cache.EhCacheProvider");
cfg.setProperty("hibernate.cache.use_second_level_cache","true");
cfg.setProperty("hibernate.cache.use_query_cache","true");
然后在映射文件中使用二级缓存
如单个实体的二级缓存
<class name="Product" table="product" >
<cache usage="read-write"/>
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="increment"/>
</id>
<property name="name" type="java.lang.String"/>
<property name="pDesc" type="java.lang.String"/>
<property name="orderId" type="java.lang.Integer"/>
</class>
最后测试
Configuration cfg = new Configuration();
cfg.configure();
//cfg.setProperty("hibernate.cache.provider_class","org.hibernate.cache.EhCacheProvider");
//cfg.setProperty("hibernate.cache.use_second_level_cache","true");
//cfg.setProperty("hibernate.cache.use_query_cache","true");
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
Transaction transaction = session.beginTransaction();
Product product = (Product)session.get(Product.class,1);
transaction.commit();
System.out.println(product.getName());
Session session2 = sessionFactory.getCurrentSession();
Transaction trasaction2 = session2.beginTransaction();
Product product2 = (Product)session2.get(Product.class,1);
trasaction2.commit();
System.out.println(product2.getName());
打印结果为:
Hibernate: select product0_.id as id0_0_, product0_.name as name0_0_, product0_.pDesc as pDesc0_0_, product0_.orderId as orderId0_0_ from product product0_ where product0_.id=?
oly
oly
可见只查询了一次数据库。
有时间得研究一下hibernate4配置缓存的情况了,呵呵
在hibernate3下面使用二级缓存的实例
设置使用二级缓存
方式1在hibernate.cfg文件中添加
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
说使用Configuration配置
cfg.setProperty("hibernate.cache.provider_class","org.hibernate.cache.EhCacheProvider");
cfg.setProperty("hibernate.cache.use_second_level_cache","true");
cfg.setProperty("hibernate.cache.use_query_cache","true");
然后在映射文件中使用二级缓存
如单个实体的二级缓存
<class name="Product" table="product" >
<cache usage="read-write"/>
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="increment"/>
</id>
<property name="name" type="java.lang.String"/>
<property name="pDesc" type="java.lang.String"/>
<property name="orderId" type="java.lang.Integer"/>
</class>
最后测试
Configuration cfg = new Configuration();
cfg.configure();
//cfg.setProperty("hibernate.cache.provider_class","org.hibernate.cache.EhCacheProvider");
//cfg.setProperty("hibernate.cache.use_second_level_cache","true");
//cfg.setProperty("hibernate.cache.use_query_cache","true");
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
Transaction transaction = session.beginTransaction();
Product product = (Product)session.get(Product.class,1);
transaction.commit();
System.out.println(product.getName());
Session session2 = sessionFactory.getCurrentSession();
Transaction trasaction2 = session2.beginTransaction();
Product product2 = (Product)session2.get(Product.class,1);
trasaction2.commit();
System.out.println(product2.getName());
打印结果为:
Hibernate: select product0_.id as id0_0_, product0_.name as name0_0_, product0_.pDesc as pDesc0_0_, product0_.orderId as orderId0_0_ from product product0_ where product0_.id=?
oly
oly
可见只查询了一次数据库。
有时间得研究一下hibernate4配置缓存的情况了,呵呵