Hibernate二级缓存

 1 @org.junit.Test
 2     public void testHibernateSecondLevelCache(){
 3         Employee employee=(Employee) session.get(Employee.class, 7499);
 4         System.out.println(employee.getName());
 5         
 6         transaction.commit();
 7         session.close();
 8         session = sessionFactory.openSession();
 9         transaction = session.beginTransaction();
10         
11         Employee employee2=(Employee) session.get(Employee.class, 7499);
12         System.out.println(employee2.getName());
13     }

现在这 这段代码就是两个session。会发送两条sql语句。

一:hibernate二级缓存应用

1:加入jar包

2:加入ehcache.xml

<ehcache>

    <!-- Sets the path to the directory where cache .data files are created.

         If the path is a Java System Property it is replaced by
         its value in the running VM.

         The following properties are translated:
         user.home - User's home directory
         user.dir - User's current working directory
         java.io.tmpdir - Default temp file path -->
    <diskStore path="java.io.tmpdir"/>


    <!--Default Cache configuration. These will applied to caches programmatically created through
        the CacheManager.

        The following attributes are required for defaultCache:

        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />

    <!--Predefined caches.  Add your cache configuration settings here.
        If you do not have a configuration for your cache a WARNING will be issued when the
        CacheManager starts

        The following attributes are required for defaultCache:

        name              - Sets the name of the cache. This is used to identify the cache. It must be unique.
        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->

    <!-- Sample cache named sampleCache1
        This cache contains a maximum in memory of 10000 elements, and will expire
        an element if it is idle for more than 5 minutes and lives for more than
        10 minutes.

        If there are more than 10000 elements it will overflow to the
        disk cache, which in this configuration will go to wherever java.io.tmp is
        defined on your system. On a standard Linux system this will be /tmp"
        -->
    <cache name="sampleCache1"
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
        />

    <!-- Sample cache named sampleCache2
        This cache contains 1000 elements. Elements will always be held in memory.
        They are not expired. -->
    <cache name="sampleCache2"
        maxElementsInMemory="1000"
        eternal="true"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        overflowToDisk="false"
        /> -->

    <!-- Place configuration for your caches following -->

</ehcache>

 

3:在hibernate.cfg.xml文件中配置需要缓存的配置。

<!-- 配置启用hibernate的二级缓存 -->
        <property name="cache.use_second_level_cache">true</property>
        <!-- 配置hibernate二级缓存使用的产品 -->
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
        <!-- 配置对哪些类使用 hibernate 的二级缓存  。在mapping的后面 -->
        <class-cache usage="read-write" class="com.hq.entities.Employee"/>

或者在Employee的 hbm.xml文件中加上

<cache usage="read-write"/>

**:对集合进行二级缓存的配置:

     <class-cache usage="read-write" class="com.hq.entities.Employee"/>
        <class-cache usage="read-write" class="com.hq.entities.Department"/>
        <collection-cache usage="read-write" collection="com.hq.entities.Department.emps"/>
 

或者在Department.hbm.xml和Employee.hbm.xml中加上

 <cache usage="read-write"/>

然后在集合中加上cache

<set name="emps" table="GG_EMP" inverse="true" lazy="true">
            <cache usage="read-write"/>
            <key>
                <column name="DEPT_ID" />
            </key>
            <one-to-many class="com.hq.entities.Employee" />
        </set>

***.  查询缓存: 默认情况下, 设置的缓存对 HQL 及 QBC 查询时无效的, 但可以通过以下方式使其是有效的

I.  在 hibernate 配置文件中声明开启查询缓存

<property name="cache.use_query_cache">true</property>

II. 调用 Query 或 Criteria 的 setCacheable(true) 方法

III. 查询缓存依赖于二级缓存

posted @ 2017-11-22 21:48  陆伟  阅读(181)  评论(0编辑  收藏  举报