使用 Hibernate 二级缓存的步骤
生活随笔
收集整理的這篇文章主要介紹了
使用 Hibernate 二级缓存的步骤
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 使用 Hibernate 二級緩存的步驟:
1). 加入二級緩存插件的 jar 包及配置文件:
I. 復制 \hibernate-release-4.2.4.Final\lib\optional\ehcache\*.jar 到當前 Hibrenate 應用的類路徑下.
II. 復制 hibernate-release-4.2.4.Final\project\etc\ehcachexml 到當前 WEB 應用的類路徑下
2). 配置 hibernate.cfg.xml?
I. ? 配置啟用 hibernate 的二級緩存
<property name="cache.use_second_level_cache">true</property>
II. ?配置hibernate二級緩存使用的產品
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
III. 配置對哪些類使用 hibernate 的二級緩存
<class-cache usage="read-write" class="com.atguigu.hibernate.entities.Employee"/>
實際上也可以在 .hbm.xml 文件中配置對哪些類使用二級緩存, 及二級緩存的策略是什么.?
2). 集合級別的二級緩存的配置
I. 配置對集合使用二級緩存
<collection-cache usage="read-write" collection="com.atguigu.hibernate.entities.Department.emps"/>
也可以在 .hbm.xml 文件中進行配置
<set name="emps" table="GG_EMPLOYEE" inverse="true" lazy="true">
<cache usage="read-write"/>
? ? <key>
? ? ? ? <column name="DEPT_ID" />
? ? </key>
? ? <one-to-many class="com.atguigu.hibernate.entities.Employee" />
</set>
II. 注意: 還需要配置集合中的元素對應的持久化類也使用二級緩存! 否則將會多出 n 條 SQL 語句.?
3). ehcache 的 配置文件: ehcache.xml
4). ?查詢緩存: 默認情況下, 設置的緩存對 HQL 及 QBC 查詢時無效的, 但可以通過以下方式使其是有效的
I. ?在 hibernate 配置文件中聲明開啟查詢緩存
<property name="cache.use_query_cache">true</property>
II. 調用 Query 或 Criteria 的 setCacheable(true) 方法
III. 查詢緩存依賴于二級緩存
1). 加入二級緩存插件的 jar 包及配置文件:
I. 復制 \hibernate-release-4.2.4.Final\lib\optional\ehcache\*.jar 到當前 Hibrenate 應用的類路徑下.
II. 復制 hibernate-release-4.2.4.Final\project\etc\ehcachexml 到當前 WEB 應用的類路徑下
2). 配置 hibernate.cfg.xml?
I. ? 配置啟用 hibernate 的二級緩存
<property name="cache.use_second_level_cache">true</property>
II. ?配置hibernate二級緩存使用的產品
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
III. 配置對哪些類使用 hibernate 的二級緩存
<class-cache usage="read-write" class="com.atguigu.hibernate.entities.Employee"/>
實際上也可以在 .hbm.xml 文件中配置對哪些類使用二級緩存, 及二級緩存的策略是什么.?
2). 集合級別的二級緩存的配置
I. 配置對集合使用二級緩存
<collection-cache usage="read-write" collection="com.atguigu.hibernate.entities.Department.emps"/>
也可以在 .hbm.xml 文件中進行配置
<set name="emps" table="GG_EMPLOYEE" inverse="true" lazy="true">
<cache usage="read-write"/>
? ? <key>
? ? ? ? <column name="DEPT_ID" />
? ? </key>
? ? <one-to-many class="com.atguigu.hibernate.entities.Employee" />
</set>
II. 注意: 還需要配置集合中的元素對應的持久化類也使用二級緩存! 否則將會多出 n 條 SQL 語句.?
3). ehcache 的 配置文件: ehcache.xml
4). ?查詢緩存: 默認情況下, 設置的緩存對 HQL 及 QBC 查詢時無效的, 但可以通過以下方式使其是有效的
I. ?在 hibernate 配置文件中聲明開啟查詢緩存
<property name="cache.use_query_cache">true</property>
II. 調用 Query 或 Criteria 的 setCacheable(true) 方法
III. 查詢緩存依賴于二級緩存
總結
以上是生活随笔為你收集整理的使用 Hibernate 二级缓存的步骤的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机没有音频驱动程序,电脑没有声音怎么
- 下一篇: Hibernate一级缓存和二级缓存详解