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

时间:2016-08-04 10:59:35

标签: caching spring-4 hibernate-5.x

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

Dim ofd as new OpenFileDialog()
// Show the File Dialog to the user and detect he pressed OK or Cancelled
if ofd.ShowDialog = Windows.Forms.DialogResult.OK
      // Always check, if the file really exists
      if IO.File.exists(ofd.FileName)
         importbox = ofd.FileName
      Else
         msgbox("File does not exist")
         Exit Sub
      End if
Else
   Exit Sub
End if

我收到以下错误

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

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

1 个答案:

答案 0 :(得分:2)

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

但是如果你愿意使用hibernate注释库来配置缓存,那么试试使用CacheConcurrencyStrategy枚举器的this approach。 您可以在Concurrency级别应用适当的Entity策略。

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

请在dev guide中找到CacheConcurrencyStrategy的更多用法。