caching - spring4.3.x如何配置 'entityCacheStrategies'

标签 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>

我收到以下错误
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

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

最佳答案

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

但是,如果您愿意使用 hibernate 注释库配置缓存,请尝试 this approach这使得使用 CacheConcurrencyStrategy枚举器。
您可以申请适当的Concurrency策略在 Entity等级。

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class Clazz{..}

请查找 CacheConcurrencyStrategy 的更多用法在 dev guide .

关于caching - spring4.3.x如何配置 'entityCacheStrategies',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38765499/

相关文章:

java - 将 org.springframework.cloud.stream.annotation 的 Spring v2 迁移到 Spring v4

java - 使用 hibernate 5 和 c3po 连接池在 java 应用程序中设置 vsession.program

php - Yii 中的模式缓存

java - Spring 4 中有多个@ComponentScan?

spring - @Qualifier 无效

postgresql - 使用 postgres 表序列而不是共享 hibernate_sequence

java - hibernate 和 Spring : Exception when trying to create a transaction

caching - Gitlab CI - 如果缓存存在,如何跳过作业

java - 如何禁用 Ajax 缓存?

ruby-on-rails - Rails 中缓存的样式表何时刷新?