hibernate二級緩衝配置學習記錄

來源:互聯網
上載者:User

1、把
    ehcache-1.5.0.jar
    backport-util-concurrent.jar
    commons-logging.jar
    加入path
2、在hibernate.cfg.xml中加入
    <!-- 配置二級緩衝供應商 -->
        <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
         <!-- 配置啟動查詢快取 -->
               <property name="hibernate.cache.use_query_cache">true</property>
3、選擇需要使用二級緩衝的類
    a、在*.hbm.xml中配置(不推薦)
        選擇需要使用二級緩衝的持久化類,設定它的二級緩衝的兵法策略
        class元素的cache子項目表明hibernate會緩衝對象的簡單屬性,單不會緩衝集合屬性
        若希望緩衝集合屬性中的元素,必須在set元素中加入cache子項目
    b、在hibernate.cfg.xml中配置(推薦)
        <!-- 要放置在 hibernate.cache.provider_class的下面-->
        <mapping  resource="cn/itcast/bean/ehcache/User.hbm.xml"/>
        <!-- 配置類層級的二級緩衝 -->
        <class-cache usage="read-write" class="cn.itcast.bean.ehcache.User"/>
4、測試二級緩衝
    /**
         * 啟用了二級緩衝,列印一條sql語句
         *     在session的以及緩衝中放置一份
         *     在sessionFactory的二級緩衝中放置一份
         */
        User u=(User) session.load(cn.itcast.bean.ehcache.User.class, 1);
        t.commit();
        session.close();
       
        session = sf.openSession();
        t = session.beginTransaction();
        /**
         * 不列印sql語句,因為開啟了二級緩衝,直接查詢sessionFactory的緩衝
         */
        User u1=(User) session.load(cn.itcast.bean.ehcache.User.class, 1);
        t.commit();
        session.close();
           

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.