Hibernate3.6 & Ehcache2.4.3配置

关于集群配置:
Ehcache分布式缓存 http://simpleframework.net/blog/v/7986.html
Ehcache集群环境配置 http://zhouree.iteye.com/blog/335623
自己成功使用ehcache集群的总结 http://www.open-open.com/lib/view/open1345651870876.html
pom.xml
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-ehcache</artifactId>
			<version>3.6.10.Final</version>
		</dependency>
		<dependency>
			<groupId>hsqldb</groupId>
			<artifactId>hsqldb</artifactId>
			<version>1.8.0.4</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-testing</artifactId>
			<version>3.6.10.Final</version>
		</dependency>
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache-core</artifactId>
			<version>2.4.3</version>
		</dependency>

applicationContext.xml
<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
		p:packagesToScan="com.pandy.ssh4.domian">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.SQLServer2008Dialect
				</prop>
				<prop key="hibernate.show_sql">false</prop>
				<prop key="hibernate.format_sql">false</prop>
				<prop key="hibernate.cache.use_query_cache">true</prop>  
                <prop key="hibernate.cache.use_second_level_cache">true</prop>  
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</prop>  
                <prop key="hibernate.cache.use_structured_entries">true</prop>  
                <prop key="hibernate.generate_statistics">true</prop>
			</props>
		</property>
	</bean>

ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
	<!--如果缓存中的对象存储超过指定的缓存数量的对象存储的磁盘地址 -->
	<diskStore path="D:/ehcache" />

	<!-- 默认cache:如果没有对应的特定区域的缓存,就使用默认缓存 -->
	<defaultCache maxElementsInMemory="10000" eternal="false"
		timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="false" />
		
		
	<!-- 指定区域cache:通过name指定,name对应到Hibernate中的区域名即可 -->
	<cache name="cn.javass.h3test.model.UserModel" eternal="false"
		maxElementsInMemory="100" timeToIdleSeconds="1200" timeToLiveSeconds="1200"
		overflowToDisk="false">
	</cache>

</ehcache> 



@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
package com.pandy.ssh4.domian;

// Generated 2013-3-7 17:34:40 by Hibernate Tools 4.0.0

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

/**
 * Table3 generated by hbm2java
 */
@SuppressWarnings("serial")
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)  
@Table(name = "TABLE3", schema = "dbo", catalog = "Test")
public class Table3 extends BasicDomian implements java.io.Serializable {

	private int id;
	private String name;
	private String password;
	private String email;

	......

}




query.setCacheable(true);//设定使用缓存
@SuppressWarnings("rawtypes")
	public void doEhcache2() {

		Session session = getCurrentSession();
		String hql = " from com.pandy.ssh4.domian.Table3";
		Query query = session.createQuery(hql);
		query.setCacheable(true);//设定使用缓存

		List list = query.list();
		System.out.println(list.size());
	}

你可能感兴趣的:(hibernate3)