读书人

jpa hibernate 上配置ehcache

发布时间: 2013-01-25 15:55:29 作者: rapoo

jpa hibernate 下配置ehcache

源:http://www.iteye.com/problems/7842

http://dxp4598.iteye.com/blog/1250512

评:

    ? ???<property name="hibernate.cache.provider_configuration" value="/ehcache.xml" />
    ? ???<property name="hibernate.cache.use_second_level_cache" value="true" />
    ? ???<property name="hibernate.cache.use_query_cache" value="true" />

    ?

    ?

    3, 对ehcache进行简单的设置(ehcache.xml)

    ?

    ????? <?xml version="1.0" encoding="UTF-8"?>
    ????? <ehcache>
    ????? <defaultCache maxElementsInMemory="1000" eternal="false"
    ????????? ?timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="false"
    ?????????? clearOnFlush="true">
    ????? </defaultCache>

    ?????? <!-- 单独对某个entity的缓存策略设置-->
    ????? <cache name="com.payment.entity.PromotionEntity" maxElementsInMemory="100"

    ?????????? eternal="false"
    ????????? ?timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="false"
    ?????????? clearOnFlush="true">
    ????? </cache>
    ???? </ehcache>

    ?

    ?

    4, JPA的Entity类中声明缓存的隔离机制

    ?

    ?

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

    ?

    ??????@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    ????? @Entity
    ????? @Table(name = "catagory")
    ????? public class CatagoryEntity extends BaseEntity { ... }

    ?

    ?

    5, 如何使用二级缓存中的对象

    ?

    ?

    ?????? 在Hibernate中可以通过org.hibernate.Query.setCacheable(true);

    ?????? 在JPA中,由于EntityManager中得到的javax.persistence.Query没有这个方法了。我们可以通过

    ?????? javax.persistence.Query.setHint(”org.hibernate.cacheable”, true);来实现读取二级缓存。

    ?

    ?

    ?

    6, 在log4j输出日志中可以看到缓存机制作用

    log4j.logger.org.hibernate.cache=debug

    ?

读书人网 >互联网

热点推荐