0

I am using spring - hibernate integration and I have hibernate.cfg.xml which has session-factory node in xml.

In persistense.xml with jpa I have used shred-cache-mode for second level caching.

I want to configure the same with hibernate config file for making it ENABLE_SELECTIVE but it doesn't seem possible.

Nimesh
  • 794
  • 3
  • 8
  • 18
  • 1
    As far as I remember, if using pure Hibernate, the default behaviour is to only cache those entities/collections with `@Cacheable` in them. What problems are you having exactly? – Galder Zamarreño Apr 27 '15 at 06:37
  • I was looking for that I want to enable cache selectively. Is it possible to configure other modes from hibernate config file? – Nimesh Apr 27 '15 at 11:14
  • AFAIK, to reiterate, I think Hibernate already enables caches selectively out of the box when running Hibernate native. I'm not aware of any other options in hibernate.cfg.xml – Galder Zamarreño Apr 27 '15 at 16:13

1 Answers1

3

I'm not sure either, but I would also assume that ENABLE_SELECTIVE is already the default, because with @Cacheable you somehow activate it selectively.

However, using spring boot according to the answer on Spring Boot + JPA2 + Hibernate - enable second level cache it seems like you can influence the cache mode using application.properties (spring boot's configuration file) with an entry like this:

spring.jpa.properties.javax.persistence.sharedCache.mode=ENABLE_SELECTIVE

Maybe you can transfer this into the XML world and use the property like this in your JPA spring configuration XML file:

<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="packagesToScan" value="com.example.persistence.model" />
      <property name="jpaVendorAdapter">
         <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
      </property>
      <property name="jpaProperties">
         <props>
            <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="javax.persistence.sharedCache.mode">ENABLE_SELECTIVE</prop>
         </props>
      </property>
</bean>

I haven't tested it, but IMO this the right way to go.

Community
  • 1
  • 1
mika
  • 2,495
  • 22
  • 30