如何在spring4.3.x中配置'entityCacheStrategies'

Ach*_*ius 5 caching spring-4 hibernate-5.x

我正在从spring3.x迁移到spring4.3.x. 我在bean创建中使用org.springframework.orm.hibernate5.LocalSessionFactoryBean如下

    <bean id="sessionFactory"
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="annotatedClasses">
        <list>
            <value>com.example.person.domain.Person</value>
        </list>
    </property>

    <property name="mappingLocations">
        <list>
            <value>classpath*:com/example/person/domain/Person.hbm.xml</value>
        </list>
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop>
            <prop key="hibernate.cache.provider_class">${cache.providerClass}</prop>
            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.memcached.dogpilePrevention">true</prop>
            <prop key="hibernate.memcached.servers">${cache.memcached.servers}</prop>
            <prop key="hibernate.memcached.keyStrategy">com.example.memcached.ExampleCacheStringKeyStrategy</prop>
            <prop key="hibernate.memcached.deviceAttributeDao.keyStrategy">com.googlecode.hibernate.memcached.HashCodeKeyStrategy</prop>
            <prop key="hibernate.memcached.EncodingDao.keyStrategy">com.googlecode.hibernate.memcached.HashCodeKeyStrategy</prop>
        </props>
    </property>

    <property name="entityCacheStrategies">
        <props>
            <prop key="com.example.person.domain.TestAttribute">read-write,_domain</prop>
        </props>
    </property>

    <property name="dataSource" ref="dataSource"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

我收到了以下错误

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'entityCacheStrategies' of bean class [org.springframework.orm.hibernate5.LocalSessionFactoryBean]: Bean property 'entityCacheStrategies' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.createNotWritablePropertyException(BeanWrapperImpl.java:242)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:423)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:280)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1514)
... 66 more
Run Code Online (Sandbox Code Playgroud)

我正在使用spring-4.3.0.RELEASE.LocalSessionFactoryBean没有entityCacheStrategies的setter.知道如何在spring4.3.x中支持这个吗?

Aka*_*hra 2

entityCacheStrategies/setEntityCacheStrategies()hibernate 5 中已弃用 的用法。可能的替代方案org.hibernate.cfg.Configuration : setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy)也不再使用。

但是,如果您愿意使用 hibernate 注释库配置缓存,那么请尝试这种使用枚举器的方法CacheConcurrencyStrategy。您可以Concurrency在级别上应用适当的策略Entity

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class Clazz{..}
Run Code Online (Sandbox Code Playgroud)

CacheConcurrencyStrategy请在开发指南中找到更多用法。