Announcement Announcement Module
Collapse
No announcement yet.
@Cacheable with spring 3.1, Hibernate 4 and eh-cache Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Cacheable with spring 3.1, Hibernate 4 and eh-cache

    Hi,

    I want to use @cachable for my JPA2-Entities. I'm using spring 3.1 and Hibernate 4.1.6.Final and I want to use eh-cache as cache.

    Although I followed the tips here:
    http://www.javacraft.org/2012/03/mig...4-ehcache.html

    I still get java.lang.NoClassDefFoundError: org/hibernate/cache/TimestampsRegion.

    Has this something to do with spring 3.1?

    Thank you.

    P.S. EntityManager:

    Code:
    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    	    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    	</bean>
    	<tx:annotation-driven transaction-manager="transactionManager" order="35"/>
    	<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    	    <property name="persistenceUnitName" value="persistenceUnit"/>
    	    <property name="dataSource" ref="dataSource"/>
    	</bean>
    persistence.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> 
            <properties>
                
                <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect"/>
    
                <!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
                <property name="hibernate.hbm2ddl.auto" value="validate"/>
                <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultComponentSafeNamingStrategy"/>
                <property name="hibernate.connection.charSet" value="UTF-8"/>
                
                
    
    				   	<property name="hibernate.cache.region.factory_class" value ="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory" />
    						<property name="hibernate.cache.use_second_level_cache"	value="true" />
    						<property name="hibernate.cache.use_query_cache" value="true" />
    
    						<property name="net.sf.ehcache.configurationResourceName" value="myehcache.xml"/>
    						
            </properties>
        </persistence-unit>
    </persistence>

  • #2

    Has this something to do with spring 3.1?
    No... And I also suggest you read the blog again, it explains what to do with this exception (you have to use the hibernate specific implementation). Either that or use a different cache factory for hibernate (check the hibernate or ehcache reference guide for more information).

    Comment


    • #3
      I did use the hobernate-eh-cache version:

      <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-ehcache</artifactId>
      <version>${hibernate.version}</version>
      </dependency>
      <!--
      <dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache-core</artifactId>
      <version>2.6.0</version>
      </dependency>
      -->

      Comment


      • #4
        Please use [ code][/code ] tags when posting xml...

        Check your dependencies and see if there isn't another one pulled in from somewhere. (Not sure if spring works with the ehcache version from hibernate).

        As mentioned before also read the ehcache documentation.

        Comment


        • #5
          I checked this serveral times, I have ehcache-core-2.4.3.jar which is a dependency of hibernate-ehcache.
          I read the docs.
          I think the problem is, that Spring 3.1 does not support jpa2 and hibernate 4.1 with eh-cache.

          Comment


          • #6

            I think the problem is, that Spring 3.1 does not support jpa2 and hibernate 4.1 with eh-cache.
            No it isn't... The error you get is an error from hibernate so your error is there... Using the same setup for a sample application so nothing is preventing you from using this combination.

            Comment


            • #7
              I cannot find any error in my setup...
              OK; I have to setup a sample project.

              Comment


              • #8
                OK; I build a minimal project and can reproduce the problem.
                I still get:
                Code:
                Caused by: java.lang.NoClassDefFoundError: org/hibernate/cache/TimestampsRegion
                	at java.lang.Class.forName0(Native Method)
                	at java.lang.Class.forName(Class.java:247)
                	at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:138)
                	at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:393)
                	at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:264)
                	at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2279)
                	at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2275)
                	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
                	at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
                	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
                	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
                	at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
                	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:268)
                	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
                This is my pom:

                Code:
                <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
                  <modelVersion>4.0.0</modelVersion>
                  <groupId>com.cn</groupId>
                  <artifactId>ehcachetest</artifactId>
                  <version>0.0.1-SNAPSHOT</version>
                  <name>ehcachetest</name>
                	<properties>
                		<java-version>1.7</java-version>
                		<org.aspectj-version>1.6.12</org.aspectj-version>
                		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                		<org.slf4j-version>1.6.2</org.slf4j-version>
                		<spring.version>3.1.1.RELEASE</spring.version>
                		<spring-security.version>3.1.1.RELEASE</spring-security.version>
                		<spring-data.version>1.1.0.RELEASE</spring-data.version>
                		<hibernate.version>4.1.6.Final</hibernate.version>
                		<junit.version>4.7</junit.version>
                	</properties>
                	
                	<dependencies>
                		<dependency>
                			<groupId>cglib</groupId>
                			<artifactId>cglib-nodep</artifactId>
                			<version>2.2.2</version>
                		</dependency>
                		<dependency>
                			<groupId>org.springframework</groupId>
                			<artifactId>spring-context</artifactId>
                			<version>${spring.version}</version>
                		</dependency>
                		<dependency>
                			<groupId>javax.transaction</groupId>
                			<artifactId>jta</artifactId>
                			<version>1.1</version>
                		</dependency>
                		<dependency>
                			<groupId>org.springframework</groupId>
                			<artifactId>spring-jdbc</artifactId>
                			<version>${spring.version}</version>
                		</dependency>
                		<dependency>
                			<groupId>org.springframework</groupId>
                			<artifactId>spring-orm</artifactId>
                			<version>${spring.version}</version>
                		</dependency>
                		<dependency>
                			<groupId>org.springframework.data</groupId>
                			<artifactId>spring-data-jpa</artifactId>
                			<version>${spring-data.version}</version>
                		</dependency>
                		<dependency>
                			<groupId>commons-pool</groupId>
                			<artifactId>commons-pool</artifactId>
                			<version>1.5.6</version>
                		</dependency>
                		<dependency>
                			<groupId>commons-dbcp</groupId>
                			<artifactId>commons-dbcp</artifactId>
                			<version>1.3</version>
                			<exclusions>
                				<exclusion>
                					<groupId>commons-logging</groupId>
                					<artifactId>commons-logging</artifactId>
                				</exclusion>
                				<exclusion>
                					<groupId>xml-apis</groupId>
                					<artifactId>xml-apis</artifactId>
                				</exclusion>
                			</exclusions>
                		</dependency>
                		<dependency>
                		    <groupId>net.sourceforge.jtds</groupId>
                		    <artifactId>jtds</artifactId>
                		    <version>1.2</version>
                		</dependency>
                		<dependency>
                			<groupId>org.hibernate</groupId>
                			<artifactId>hibernate-core</artifactId>
                			<version>${hibernate.version}</version>
                		</dependency>
                		<dependency>
                			<groupId>org.hibernate</groupId>
                			<artifactId>hibernate-entitymanager</artifactId>
                			<version>${hibernate.version}</version>
                			<exclusions>
                				<exclusion>
                					<groupId>cglib</groupId>
                					<artifactId>cglib</artifactId>
                				</exclusion>
                				<exclusion>
                					<groupId>dom4j</groupId>
                					<artifactId>dom4j</artifactId>
                				</exclusion>
                			</exclusions>
                		</dependency>
                		<dependency>
                			<groupId>org.hibernate.javax.persistence</groupId>
                			<artifactId>hibernate-jpa-2.0-api</artifactId>
                			<version>1.0.1.Final</version>
                		</dependency>
                		<dependency>
                			<groupId>org.hibernate</groupId>
                			<artifactId>hibernate-ehcache</artifactId>
                			<version>${hibernate.version}</version>
                		</dependency>
                		<dependency>
                			<groupId>org.slf4j</groupId>
                			<artifactId>slf4j-api</artifactId>
                			<version>${org.slf4j-version}</version>
                		</dependency>
                		<dependency>
                			<groupId>org.slf4j</groupId>
                			<artifactId>jcl-over-slf4j</artifactId>
                			<version>${org.slf4j-version}</version>
                		</dependency>
                		<dependency>
                			<groupId>org.slf4j</groupId>
                			<artifactId>slf4j-log4j12</artifactId>
                			<version>${org.slf4j-version}</version>
                			<scope>runtime</scope>
                		</dependency>
                		<dependency>
                			<groupId>log4j</groupId>
                			<artifactId>log4j</artifactId>
                			<version>1.2.15</version>
                			<exclusions>
                				<exclusion>
                					<groupId>javax.mail</groupId>
                					<artifactId>mail</artifactId>
                				</exclusion>
                				<exclusion>
                					<groupId>javax.jms</groupId>
                					<artifactId>jms</artifactId>
                				</exclusion>
                				<exclusion>
                					<groupId>com.sun.jdmk</groupId>
                					<artifactId>jmxtools</artifactId>
                				</exclusion>
                				<exclusion>
                					<groupId>com.sun.jmx</groupId>
                					<artifactId>jmxri</artifactId>
                				</exclusion>
                			</exclusions>
                			<!-- <scope>runtime</scope> -->
                		</dependency>		
                		<dependency>
                			<groupId>junit</groupId>
                			<artifactId>junit</artifactId>
                			<version>${junit.version}</version>
                			<scope>test</scope>
                		</dependency>
                		<dependency>
                			<groupId>org.springframework</groupId>
                			<artifactId>spring-test</artifactId>
                			<version>${spring.version}</version>
                			<scope>test</scope>
                		</dependency>
                	</dependencies>
                
                </project>
                persistence.xml

                Code:
                <?xml version="1.0" encoding="UTF-8" standalone="no"?>
                <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
                <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
                        <provider>org.hibernate.ejb.HibernatePersistence</provider>
                        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> 
                        <properties>
                            <!-- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/> -->
                            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect"/>
                
                            <!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
                            <property name="hibernate.hbm2ddl.auto" value="update"/>
                            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultComponentSafeNamingStrategy"/>
                            <property name="hibernate.connection.charSet" value="UTF-8"/>
                            <property name="hibernate.cache.use_second_level_cache"	value="true" />
                            <property name="hibernate.cache.use_query_cache" value="true" />
                            <property name="net.sf.ehcache.configurationResourceName" value="myehcache.xml"/>
                        </properties>
                    </persistence-unit>
                </persistence>
                applicationContext.xml, namespaces stripped, message too big...

                Code:
                <?xml version="1.0" encoding="UTF-8" standalone="no"?>
                <beans
                	<context:property-placeholder location="classpath*:META-INF/spring/*.properties" />
                
                	<jpa:repositories base-package="com.cn" />
                		
                	<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
                	    <property name="driverClassName" value="${database.driverClassName}"/>
                	    <property name="url" value="${database.url}"/>
                	    <property name="username" value="${database.username}"/>
                	    <property name="password" value="${database.password}"/>
                	    <property name="testOnBorrow" value="true"/>
                	    <property name="testOnReturn" value="true"/>
                	    <property name="testWhileIdle" value="true"/>
                	    <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
                	    <property name="numTestsPerEvictionRun" value="3"/>
                	    <property name="minEvictableIdleTimeMillis" value="1800000"/>
                	    <property name="validationQuery" value="SELECT 1"/>
                	</bean>
                	<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
                	    <property name="entityManagerFactory" ref="entityManagerFactory"/>
                	</bean>
                	<tx:annotation-driven transaction-manager="transactionManager" order="35"/>
                	<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
                	    <property name="persistenceUnitName" value="persistenceUnit"/>
                	    <property name="dataSource" ref="dataSource"/>
                	</bean>
                		    
                </beans>

                Comment


                • #9
                  Damned...

                  I have to use org.hibernate.cache.ehcache.SingletonEhCacheRegion Factory in persistence.xml not net.sf.ehcache.hibernate.SingletonEhCacheRegionFac tory.

                  As stated in the article in my first posting...

                  Comment

                  Working...
                  X