Upgrading to jcache creating issues

I have the folowing hibernate (legacy (non-JPA)) dependencies defined while using Spring 5.X version

<dependency>
	<groupId>org.hibernate</groupId>
	<artifactId>hibernate-core</artifactId>
	<version>5.6.5.Final</version>
</dependency>
<dependency>
	<groupId>org.hibernate</groupId>
	<artifactId>hibernate-ehcache</artifactId>
	<version>5.6.5.Final</version>
</dependency>
<dependency>
	<groupId>net.sf.ehcache</groupId>
	<artifactId>ehcache</artifactId>
	<version>2.10.9.2</version>
	<exclusions>
		<exclusion>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
		</exclusion>
	</exclusions>
</dependency>

Since I’m moving to Spring 6.X framework. I got it changed to following:

<dependency>
	<groupId>org.hibernate</groupId>
	<artifactId>hibernate-core-jakarta</artifactId>
	<version>5.6.15.Final</version>
</dependency>
<dependency>
	<groupId>org.hibernate.orm</groupId>
	<artifactId>hibernate-jcache</artifactId>
	<version>6.2.6.Final</version>
</dependency>
<dependency>
	<groupId>org.ehcache</groupId>
	<artifactId>ehcache</artifactId>
	<version>3.10.0</version>
	<exclusions>
		<exclusion>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
		</exclusion>
	</exclusions>
</dependency>		

And in the applicationContext.xml, I got the following :

<bean id="ehcache"
		class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
		p:config-location="/WEB-INF/applicationContext-ehcache.xml"
		p:shared="true" />
<bean id="cacheManager"
		class="org.springframework.cache.ehcache.EhCacheCacheManager"
		p:cache-manager-ref="ehcache" />

changed to the following :

<bean id="ehcache"
		class="org.springframework.cache.jcache.JCacheManagerFactoryBean"
		p:config-location="/WEB-INF/applicationContext-ehcache.xml"
		p:shared="true" />
<bean id="cacheManager"
		class="org.springframework.cache.jcache.JCacheCacheManager"
		p:cache-manager-ref="ehcache" />		

Do I need to make any changes in the following existing applicationContext-ehcache.xml ?

<?xml version="1.0" encoding="UTF-8"?>

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="classpath:ehcache.xsd" updateCheck="false">

	<diskStore path="java.io.tmpdir" />
	<defaultCache maxElementsInMemory="1000" eternal="false"
		timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
	<cache name="org.hibernate.cache.StandardQueryCache"
		maxElementsInMemory="50" eternal="false" timeToLiveSeconds="120"
		overflowToDisk="true" />
	<cache name="org.hibernate.cache.UpdateTimestampsCache"
		maxElementsInMemory="5000" eternal="true" overflowToDisk="true" />

	<cache name="com.pending.model.Test" maxElementsInMemory="10000"
		eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="1200"
		overflowToDisk="false" />
	<cache name="com.pending.model.TestGroup" maxElementsInMemory="1000"
		eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="1200"
		overflowToDisk="false" />
	<cache name="com.pending.model.Type"
		maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="600"
		timeToLiveSeconds="1200" overflowToDisk="false" />
</ehcache> 		

I haven’t made any changes yet but I am getting all sorts of errors like following:

[INFO] ERROR - ContextLoader.initWebApplicationContext(308) | Context initialization failed
[INFO] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceSTR' defined in ServletContext 
resource [/WEB-INF/applicationContext-hibernate.xml]: Error creating bean with name 'org.springframework.cache.config.internalCacheAdvisor': Cannot resolve reference to bean 
'org.springframework.cache.annotation.AnnotationCacheOperationSource#0' while setting bean property 'cacheOperationSource'

[INFO] Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with 
name 'org.springframework.cache.config.internalCacheAdvisor': Cannot resolve reference to 
bean 'org.springframework.cache.annotation.AnnotationCacheOperationSource#0' while setting bean property 'cacheOperationSource'

Here’s how the bean in applicationContext-hibernate.xml looks like:


    <bean id="dataSourceSTR"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName">
    			<value>#{T(com.sql.DataSourceData).getDataSourceData('str').getProperty(T(com.sql.DataSourceData).DRIVER)}</value>
    		</property>
    		<property name="url">
    			<value>#{T(com.sql.DataSourceData).getDataSourceData('str').getProperty(T(com.sql.DataSourceData).URL)}</value>
    		</property>
    		<property name="username">
    			<value>#{T(com.sql.DataSourceData).getDataSourceData('str').getProperty(T(com.sql.DataSourceData).USERNAME)}</value>
    		</property>
    		<property name="password">
    			<value>#{T(com.sql.DataSourceData).getDataSourceData('str').getProperty(T(com.sql.DataSourceData).PASSWORD)}</value>
    		</property>
    	</bean>

Similarly there are other errors as well but if I can get the above one fixed, I think other will follow the same. What am I doing wrong here? If for some reason I should not use org.hibernate.orm for jcache, what could be an alternative ? Thanks!

All hibernate artifacts have to use the same version.

OK, so I am proceeding with following versions then:


<dependency>
	<groupId>org.hibernate</groupId>
	<artifactId>hibernate-core</artifactId>
	<version>6.1.0.Final</version>
</dependency>
<dependency>
	<groupId>org.hibernate</groupId>
	<artifactId>hibernate-jcache</artifactId>
	<version>6.1.0.Final</version>
</dependency>
<dependency>
	<groupId>org.ehcache</groupId>
	<artifactId>ehcache</artifactId>
	<version>3.10.0</version>
	<exclusions>
		<exclusion>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
	<groupId>org.hibernate.validator</groupId>
	<artifactId>hibernate-validator</artifactId>
	<version>6.1.0.Final</version>
</dependency>

Question #1: Is it looking good now?

Question #2:

After making above changes:

Hibernate ORM 6.1 is not maintained anymore. I would suggest you to upgrade to the latest Spring version and use whatever Hibernate ORM version that ships with. Then add dependencies for JCache.
I have no idea about what changed in EHCache 3, you will have to ask these questions in the forum of that project.

OK, I think I will have to stick with the hibernate-core-jakarta version that I was using before because I noticed that in my applicationContext-hibernate.xml, I do have org.springframework.orm.hibernate5.LocalSessionFactoryBean used like the following:

<bean id="sessionFactorySTR"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSourceSTR" />
		</property>
		<property name="mappingResources">
			<list>
				<value>com/pending/model/TestOne.hbm.xml</value>
				<value>com/pending/model/TestTwo.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.SQLServer2012Dialect
				</prop>
				<prop key="hibernate.show_sql">false</prop>
				<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
				</prop>
				<prop key="hibernate.cache.use_second_level_cache">true</prop>
			</props>
		</property>
	</bean>

And If I look at the documentation here (LocalSessionFactoryBean (Spring Framework 6.1.10 API)), it says that NOTE: Hibernate ORM 6.x is officially only supported as a JPA provider. Please use LocalContainerEntityManagerFactoryBean with JpaTransactionManager there instead. So as I am understanding it, I can’t use it since I will have to switch from non-JPA to JPA completey as mentioned in the note above.

Just wanted to make sure I’m understanding everything correctly here if someone can chime in? Thanks!