hibernate 4 with spring 4 - second levl caching不能处理一对多的映射

时间:2014-09-24 21:33:23

标签: spring hibernate caching

我有一个Hibernate 4和Spring 4的应用程序我有两个表有一对多关联,但是二级缓存只是不起作用我hava注释类如下

头等舱

@Entity
    @Table(name = "UN_COURSE_INFORMATION", catalog = "ED")
    @Cache(usage=CacheConcurrencyStrategy.READ_WRITE, region="courses")
    public class CourseInformation extends AbstractEntity {


    ......


    @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "CATEGORY_ID", nullable = false)
        @JsonBackReference
        @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
        private CourseCategory courseCategory;

    // other fields and getter setters 
    }

第二课

@Entity
    @Table(name = "UN_COURSE_CATEGORY", catalog = "ED")
    @JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
    @Cache(usage=CacheConcurrencyStrategy.READ_WRITE, region="courses")
    public class CourseCategory extends AbstractEntity {
    ......

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "courseCategory")
        @JsonManagedReference
        @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
        private Set<CourseInformation> courseInformations = new HashSet<CourseInformation>(
                0);


    // other fields and getter setters 
    }

Spring HIbernate缓存配置

<property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
                </prop>
                <prop key="net.sf.ehcache.configurationResourceName">ouehcache.xml</prop>
                <prop key="hibernate.generate_statistics">true</prop>
            </props>
        </property>

chcache配置

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
    monitoring="autodetect" dynamicConfig="true">

    <diskStore path="java.io.tmpdir/ehcache" />

    <defaultCache maxEntriesLocalHeap="10000" eternal="false"
        timeToIdleSeconds="120" timeToLiveSeconds="1200" diskSpoolBufferSizeMB="30"
        maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU" statistics="true">
        <persistence strategy="localTempSwap" />
    </defaultCache>

    <cache name="courses" maxEntriesLocalHeap="100" eternal="false"
        timeToIdleSeconds="5" timeToLiveSeconds="1200">
        <persistence strategy="localTempSwap" />
    </cache>

    <cache name="org.hibernate.cache.internal.StandardQueryCache"
        maxEntriesLocalHeap="5" eternal="false" timeToLiveSeconds="1200">
        <persistence strategy="localTempSwap" />
    </cache>

    <cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
        maxEntriesLocalHeap="5000" eternal="true">
        <persistence strategy="localTempSwap" />
    </cache>
</ehcache>

请尽快提出解决方案,谢谢。

0 个答案:

没有答案