hibernate二级缓存配置问题?



 在hibernate中使用二级缓存的配置如下:

一、主配置文件

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
	<session-factory>

		<!-- <property name="connection.datasource">java:comp/env/jdbc/testDB</property> -->
		<property name="current_session_context_class">thread</property>
		<property name="dialect">
			org.hibernate.dialect.OracleDialect
    	</property>
		<property name="connection.url">
			jdbc:oracle:thin:@localhost:1521:XE
    	</property>
		<property name="connection.username">db_ywbz</property>
		<property name="connection.password">db_ywbz</property>
		<property name="connection.driver_class">
			oracle.jdbc.driver.OracleDriver
    	</property>

		<!-- 将hibernate底层执行的sql语句从控制台显示 -->
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>

		<!-- 开启二级缓存 -->
		<property name="hibernate.cache.use_second_level_cache">
			true
    	</property>
		<!-- 指定二级缓存组件 -->
		<property name="hibernate.cache.provider_class">
			net.sf.ehcache.hibernate.EhCacheProvider
    	</property>
    	<!-- 指定开启查询缓存 -->
    	<property name="hibernate.cache.use_query_cache">true</property>
    	

		<mapping resource="com/cn/fangxin/vo/User.hbm.xml" />
		<mapping resource="com/cn/fangxin/vo/LoginLog.hbm.xml" />
		<mapping resource="com/cn/fangxin/vo/AdminInfo.hbm.xml" />
		<mapping resource="com/cn/fangxin/vo/Role.hbm.xml" />
		<mapping resource="com/cn/fangxin/vo/Book.hbm.xml" />
		<mapping resource="com/cn/fangxin/vo/Car.hbm.xml" />
		<mapping resource="com/cn/fangxin/vo/Product.hbm.xml" />
		<mapping resource="com/cn/fangxin/vo/Bar.hbm.xml" />
		<mapping resource="com/cn/fangxin/vo/Attachment.hbm.xml" />
	</session-factory>
</hibernate-configuration>

 二、ehcache.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
参数的含义分别是:
a.maxElementInMemory表示该缓存中可以放置多少个对象,此处为1000个,根据内存的多少可以配置
b.eternal表示是否设置这些放入二级缓存的数据对象为永久的(即放入即保存,不再清除)一般都为false
c.timeToIdleSeconds=120表示如果120秒内,放入的对象没有被再次访问到,就清除出去
d.timeToLiveSeconds=120表示对象在缓存中存活的时间,一个对象进入到本缓存中120秒后,就会自动被清除(一般	设置的时间会比timeToIdleSeconds时间长),设置此属性是为了让更多活跃的对象进入到缓存中来。
e.overfloToDisk="true"表示如果活跃对象已经超出maxElementInMemory设置的最大值时,超出的对象要被写入到硬盘上保存下来,用于缓解活跃用户较多的情况。
-->
<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="1000"
        eternal="false"
        timeToIdleSeconds="1"
        timeToLiveSeconds="1"
        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="2000"
        eternal="false"
        timeToIdleSeconds="1"
        timeToLiveSeconds="1"
        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>

三、hbm配置文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.cn.fangxin.vo.User" table="T_USER">
    	<cache usage="read-only" region="sampleCache1"/>
        <id name="userId" column="user_id" type="integer">
            <generator class="sequence">
                <param name="sequence">USER_ID_SEQ</param>
            </generator>
        </id>
        <property name="userName" column="user_name" 
            type="string">
        </property>
        <property name="credits" column="credits" 
            type="integer">
        </property>
        <property name="password" column="password" 
            type="string">
        </property>
        <property name="lastVisit" column="last_visit" 
            type="date">
        </property>
        <property name="lastIp" column="last_ip" 
            type="string">
        </property>
        <set name="loginLogs" cascade="all" inverse="true">
        	<key><column name="user_id"></column></key>
        	<one-to-many class="com.cn.fangxin.vo.LoginLog"/>
        </set>
    </class>
    <!--  这种方式应该是针对原生态SQL语句
    <sql-query name="findUserByUserName">
    	<![CDATA[
    		from User a where a.userName = ?
    	]]>
    </sql-query>
    -->
    <query name="findUserByUserName">
    	<![CDATA[
    		from User a where a.userName = ?
    	]]>
    </query>
</hibernate-mapping>

这里我有几个疑问,就是当我的ehcache.xml配置文件不在src文件夹下面的时候,默认只会加载ehcache-1.2.3.jar里面的默认配置文件,例如下图:

 

 

 然后我配置:    <property name="hibernate.cache.provider_configuration_file_resource_path">config/ehcache.xml</property>

或者<property name="hibernate.cache.provider_configuration_file_resource_path">target/classes/config</property>

始终不生效,一定要在resources下面才行,有知道的朋友麻烦告知一下,有没有办法解决这个问题。

 

猜你喜欢

转载自tommy-lu.iteye.com/blog/2275533
今日推荐