Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure 'entityCacheStrategies' in spring4.3.x

I am migrating from spring3.x to spring4.3.x. I am using org.springframework.orm.hibernate5.LocalSessionFactoryBean in bean creation as follows

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

I am getting following error

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

I am using spring-4.3.0.RELEASE. The LocalSessionFactoryBean doesn't have setter for entityCacheStrategies. Any idea how to support this in spring4.3.x?

like image 503
Achaius Avatar asked Oct 29 '22 22:10

Achaius


1 Answers

The usage of entityCacheStrategies/setEntityCacheStrategies() has been deprecated in hibernate 5. A probable alternative from org.hibernate.cfg.Configuration : setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy)is also no longer in use.

But if you're open to configuring the cache using hibernate annotations library, then try this approach which makes the use of CacheConcurrencyStrategyenumerator. You can apply appropriate Concurrency strategy at Entity level.

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

Please find more usages of CacheConcurrencyStrategy in the dev guide.

like image 100
Akash Mishra Avatar answered Nov 22 '22 04:11

Akash Mishra