2016-08-04 17 views
4

我正在從spring3.x遷移到spring4.3.x。我在bean創建使用org.springframework.orm.hibernate5.LocalSessionFactoryBean如下如何在spring4.3.x中配置'entityCacheStrategies'

<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 

我使用的彈簧4.3.0.RELEASE。 LocalSessionFactoryBean沒有針對entityCacheStrategies的setter。任何想法如何在spring4.3.x中支持這個?

回答

1

entityCacheStrategies/setEntityCacheStrategies()的使用已在hibernate 5中棄用。org.hibernate.cfg.Configuration : setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy)的可能備選方案也不再使用。

但是,如果您打算使用hibernate註釋庫配置緩存,那麼請嘗試使用CacheConcurrencyStrategy枚舉器的this approach。 您可以在Entity級別適用Concurrency策略。

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

請找到CacheConcurrencyStrategydev guide更多的用途。

相關問題