Hibernate中使用2级缓存
在hibernate.cfg.xml中加入缓存支持"hibernate.cache.use_query_cache">true"hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider在src目录下的ehcache.xml中配置如下信息 如果该路径是 Java 系统参数,当前虚拟机会重新赋值。
- 在hibernate.cfg.xml中加入缓存支持
- <prop key="hibernate.cache.use_query_cache">true</prop>
- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
- 在src目录下的ehcache.xml中配置如下信息
- <ehcache>
- <!-- 设置缓存文件 .data 的创建路径。
- 如果该路径是 Java 系统参数,当前虚拟机会重新赋值。
- 下面的参数这样解释:
- user.home – 用户主目录
- user.dir – 用户当前工作目录
- java.io.tmpdir – 默认临时文件路径 -->
- <diskStore path="user.home/alcor/hibernate" />
- <!--上面的设置将会在用户的home目录下产生alcor/hibernate的目录,目录下有以配置的cache的名称命名的.data文件-->
- <!-- 缺省缓存配置。CacheManager 会把这些配置应用到程序中。
- 下列属性是 defaultCache 必须的:
- maxInMemory - 设定内存中创建对象的最大值。
- eternal - 设置元素(译注:内存中对象)是否永久驻留。如果是,将忽略超
- 时限制且元素永不消亡。
- timeToIdleSeconds - 设置某个元素消亡前的停顿时间。
- 也就是在一个元素消亡之前,两次访问时间的最大时间间隔值。
- 这只能在元素不是永久驻留时有效(译注:如果对象永恒不灭,则
- 设置该属性也无用)。
- 如果该值是 0 就意味着元素可以停顿无穷长的时间。
- timeToLiveSeconds - 为元素设置消亡前的生存时间。
- 也就是一个元素从构建到消亡的最大时间间隔值。
- 这只能在元素不是永久驻留时有效。
- overflowToDisk - 设置当内存中缓存达到 maxInMemory 限制时元素是否可写到磁盘
- 上。
- -->
- <cache name="org.hibernate.cache.StandardQueryCache"
- maxElementsInMemory="10000"
- eternal="false"
- timeToIdleSeconds="300"
- timeToLiveSeconds="4200"
- overflowToDisk="true"
- />
- <!-- Sample cache named sampleCache2
- This cache contains 1000 elements. Elements will always be held in memory.
- They are not expired. -->
- <cache name="org.hibernate.cache.UpdateTimestampsCache"
- maxElementsInMemory="5000"
- eternal="true"
- timeToIdleSeconds="0"
- timeToLiveSeconds="0"
- overflowToDisk="false"
- />
- <defaultCache maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="120"
- timeToLiveSeconds="120" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" />
- <cache name="menuCache" maxElementsInMemory="10000" eternal="true" overflowToDisk="true" />
- <!-- See http://ehcache.sourceforge.net/documentation/#mozTocId258426 for how to configure caching for your objects -->
- </ehcache>
- 将自己的缓存model加进ehcache.xml里
使用jpa注释的方式:
- package com.alcor.web.hibernate;
- import java.util.HashSet;
- import java.util.Set;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.Id;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- import org.hibernate.annotations.Cache;
- import org.hibernate.annotations.CacheConcurrencyStrategy;
- /**
- * AlcorTCountries entity. @author MyEclipse Persistence Tools
- */
- @Entity
- @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
- @Table(name = "alcor_t_countries", catalog = "alcorweb")
- public class AlcorTCountries implements java.io.Serializable {
- // Fields
- private String iso2;
- private String iso3;
- private short isono;
- private String country;
- private String region;
- private String currency;
- private String currencyCode;
- private Set<AlcorTProvinces> alcorTProvinceses = new HashSet<AlcorTProvinces>(
- 0);
- // Constructors
- /** default constructor */
- public AlcorTCountries() {
- }
- /** minimal constructor */
- public AlcorTCountries(String iso2, String iso3, short isono, String country) {
- this.iso2 = iso2;
- this.iso3 = iso3;
- this.isono = isono;
- this.country = country;
- }
- /** full constructor */
- public AlcorTCountries(String iso2, String iso3, short isono,
- String country, String region, String currency,
- String currencyCode, Set<AlcorTProvinces> alcorTProvinceses) {
- this.iso2 = iso2;
- this.iso3 = iso3;
- this.isono = isono;
- this.country = country;
- this.region = region;
- this.currency = currency;
- this.currencyCode = currencyCode;
- this.alcorTProvinceses = alcorTProvinceses;
- }
- // Property accessors
- @Id
- @Column(name = "ISO2", unique = true, nullable = false, length = 2)
- public String getIso2() {
- return this.iso2;
- }
- public void setIso2(String iso2) {
- this.iso2 = iso2;
- }
- @Column(name = "ISO3", nullable = false, length = 3)
- public String getIso3() {
- return this.iso3;
- }
- public void setIso3(String iso3) {
- this.iso3 = iso3;
- }
- @Column(name = "ISONo", nullable = false)
- public short getIsono() {
- return this.isono;
- }
- public void setIsono(short isono) {
- this.isono = isono;
- }
- @Column(name = "Country", nullable = false, length = 100)
- public String getCountry() {
- return this.country;
- }
- public void setCountry(String country) {
- this.country = country;
- }
- @Column(name = "Region", length = 100)
- public String getRegion() {
- return this.region;
- }
- public void setRegion(String region) {
- this.region = region;
- }
- @Column(name = "Currency", length = 100)
- public String getCurrency() {
- return this.currency;
- }
- public void setCurrency(String currency) {
- this.currency = currency;
- }
- @Column(name = "CurrencyCode", length = 3)
- public String getCurrencyCode() {
- return this.currencyCode;
- }
- public void setCurrencyCode(String currencyCode) {
- this.currencyCode = currencyCode;
- }
- @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
- @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "alcorTCountries")
- public Set<AlcorTProvinces> getAlcorTProvinceses() {
- return this.alcorTProvinceses;
- }
- public void setAlcorTProvinceses(Set<AlcorTProvinces> alcorTProvinceses) {
- this.alcorTProvinceses = alcorTProvinceses;
- }
- }
-
- @Cache(
- CacheConcurrencyStrategy usage(); (1)
- String region() default ""; (2)
- String include() default "all"; (3)
- )
(1) usage: the given cache concurrency strategy (NONE, READ_ONLY, NONSTRICT_READ_WRITE, READ_WRITE, TRANSACTIONAL)
(2) region (optional): the cache region (default to the fqcn of the class or the fq role name of the collection)
(3) include (optional): all to include all properties, non-lazy to only include non lazy properties (default all).
- 启动Tomcat,如发现如下错误
Could not find configuration [org.hibernate.cache.UpdateTimestampsCache]; using defaults.
Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults.
去安装第二步中的缺省模块。
更多推荐
所有评论(0)