昨天下载了一个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配置缓存的情况了,呵呵
分享到:
相关推荐
<property name="hibernate.cache.use_second_level_cache">true <!-- 查询的二级缓存配置 --> <property name="hibernate.cache.use_query_cache">true <property name="cache.provider_class">org....
## to use the second-level cache with JTA, Hibernate must be able to obtain the JTA TransactionManager #hibernate.transaction.manager_lookup_class org.hibernate.transaction....
Creating a Second-Level Bootloader for FLASH
Unfortunately, compiling is also a technical job, and many people can't, so I've collated the compiled results for them. image ############ NOTE for n2n_v2 (https://github.com/ntop/n2n): 2019-11-...
◾Fixed an issue with the faulty setting of the maximum application current for the second drive in dual drive configurations ◾Fixed an issue with Position Control configurations using an absolute ...
Manning.Hello.World_.Mar.2009.rar
信息安全_数据安全_Vote_of_No_Confidence_-_Second_F 区块链 APT 移动安全 区块链 系统安全
To ensure proper and safe functionality, the development has to consider not only electronics, but also mechanics on product and vehicle level. A holistic and traceable approach for risk analysis, ...
这是一本围绕 Web GIS 平台 ArcGIS 的手册。然而,ArcGIS 不仅仅是在线运行的制图软件。它实际上是一套集地理数据、地图和应用程序的查找、使用、创建和共享功能于一身的完整系统,专为实现特定目标而打造。
Tomcat启动报错 java.lang.ClassNotFoundException: org.apache.jsp.index_jsp,检查你的项目中是否存在jsp-api.jar,jstl-api.jar,servlet-api.jar。
Learning-Python-Design-Patterns-Second-Edition.pdf
马車2021.5.20 SpianlHDL 直播分享PPT 讨论当前IC设计当中存在的效率问题,分享SpinalHDL在IC前端设计中的优势,对比SpinalHDL 和Chisel的差异,以及基于Scala的硬件开发方法学在芯片设计中的应用和意义...直播回放:...
Baldi_-_Bioinformatics_The[1].Machine.Learning.Approach,.Second.Edition 这是一本关于生物机器学习的书籍,希望对大家有帮助
However, when successful, analytical approaches allow us to determine general laws that are not only important in themselves but also serve as benchmarks for subsequent numerical simulations and ...
NULL 博文链接:https://8366.iteye.com/blog/470815
The first mode is the Analyze mode in which all GUI tools in MEGA are enabled and visual results explorers are available for tasks such as editing sequence alignments and viewing phylogenies. This is...
apr-1 util,开发第三方库,在secondlife中用到
This book is written for programming students, intermediate-to-advanced programmers, and low-level programming enthusiasts. The prerequisites are a basic understanding of binary and hexadecimal ...
That is why Sams is excited to bring you the latest Linux kernel development information from a Novell insider in the second edition of Linux Kernel Development. This authoritative, practical guide ...
The processors share a second level cache, and maintain coherence at the L1 cache with a write-invalidate policy. The L2 cache is two-way set associative, with a block size of 8 words, and a total ...