Hibernate的二級緩衝(SessionFaction的外置緩衝)-----Helloword

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   使用   ar   檔案   sp   

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\ehcache.xml 到當前 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 檔案中配置對哪些類使用二級緩衝, 及二級緩衝的策略是什麼. (優先推薦使用這種)
在<class>的標籤裡加一個這樣的標籤<cache usage="read-write"/>
測試代碼:

@Test    public void TestSecondLevelCache(){        Employee employee=(Employee) session.get(Employee.class, 10);        System.out.println(employee.getName());        transaction.commit();        session.close();                session=sessionFactory.openSession();        transaction=session.beginTransaction();                Employee employee2=(Employee) session.get(Employee.class, 10);        System.out.println(employee2.getName());            }

測試效果:

INFO: HHH000232: Schema update completeHibernate:     select        employee0_.ID as ID1_1_0_,        employee0_.NAME as NAME2_1_0_,        employee0_.SALARY as SALARY3_1_0_,        employee0_.EMAIL as EMAIL4_1_0_,        employee0_.DEPT_ID as DEPT_ID5_1_0_     from        GG_EMPLOYEE employee0_     where        employee0_.ID=?JJJJ

 

Hibernate的二級緩衝(SessionFaction的外置緩衝)-----Helloword

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.