在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
可见只查询了一次数据库。
分享到:
相关推荐
<property name="hibernate.cache.use_second_level_cache">true <!-- 查询的二级缓存配置 --> <property name="hibernate.cache.use_query_cache">true <property name="cache.provider_class">org....
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.use_query_cache">true ``` 接下来,我们需要在实体类上添加`@Cacheable`注解,或者在映射文件中设置 `<cache ...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory ``` 接下来,我们需要为特定的实体类...
## 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....
- `<property name="hibernate.cache.use_second_level_cache">`: 是否启用二级缓存。 3. **持久化类映射**: - `<mapping resource="entity/Employee.hbm.xml">`: 指定实体类的映射文件,如`Employee.hbm.xml`。...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory ``` 接下来,我们要为想要缓存的实体...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.region.factory_class">org.hibernate.cache.jbc2.JBossCacheRegionFactory ``` 3. 配置JBoss Caching:创建一个...
" Spring+Hibernate 使用 Ehcache 作为 Second-Level Cache" Spring 和 Hibernate 是 Java Web 应用程序开发中两个非常重要的技术栈。Spring 是一个轻量级的控制反转(IoC)容器,提供了一个框架来管理 Java 对象...
- `hibernate.cache.use_second_level_cache`:开启二级缓存,提高数据访问效率。 - `hibernate.cache.provider_class`:指定二级缓存提供商,这里是EhCache,一个广泛使用的Java缓存库。 - `hibernate.cache.use...
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-...
3. **Second-Level Cache**:此版本优化了二级缓存的使用,提升了数据访问的效率。开发者可以通过注解或者配置文件启用缓存,并选择合适的缓存提供者,如Ehcache或Infinispan。 4. **JPA 2.1兼容性**:Hibernate ...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.use_query_cache">true <!-- Hibernate4 Ehcache 配置 --> <property name="hibernate.cache.region.factory_...
- `hibernate.cache.use_second_level_cache`: 是否开启二级缓存,默认为`false`。 3. **事务管理** - `hibernate.transaction.factory_class`: 事务工厂类,如`org.hibernate.transaction....
◾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 ...
`hibernate.cache.use_query_cache`和`hibernate.cache.use_second_level_cache`分别开启了查询缓存和二级缓存。最后,通过`class-cache`元素配置了`EntityClass`的缓存策略,这里使用了"read-write",表示读写操作...
spring.jpa.hibernate.cache.use_second_level_cache=true spring.jpa.hibernate.cache.region.factory_class=org.hibernate.cache.redis.hibernate5.SingletonRedisRegionFactory ``` 4. **实体类注解**: ...
<property name="hibernate.cache.use_second_level_cache">true <property name="hibernate.cache.use_query_cache">true <property name="hibernate.ejb.entitymanager_factory_name">hibernateSessionFactory ```...
mmdetection3d示例程序中预训练模型,用于测试和验证mmdetection3d是否安装成功,预训练模型为SECOND网络模型,数据集为KITTI数据集,模型名称为:hv_second_secfpn_6x8_80e_kitti-3d-car_20200620_230238-393f000c....
- **Second-Level and Query Cache**: Describes how to enable and configure Hibernate's second-level cache and query cache for improved performance. - **Query Language Substitution**: Explains how to ...