Sunday, April 3, 2016

Spring 3 + Hibernate 4 config


I faced some dependency issue:

Could not load requested class : org.hibernate.cache.ehcache.EhCacheRegionFactory

Thanks to
http://stackoverflow.com/questions/11905722/java-lang-noclassdeffounderror-org-hibernate-cache-entityregion-configuring-ehc
and its reference
http://web.archive.org/web/20130117102553/http://www.javacraft.org/2012/03/migrate-to-hibernate-4-ehcache.html

pom.xml


<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>myssh</groupId>
 <artifactId>myssh</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.build.mainClass>com.gvace.main</project.build.mainClass>
  <java.version>1.7</java.version>
  <spring.version>3.2.16.RELEASE</spring.version>
  <hibernate.version>4.1.7.Final</hibernate.version>

 </properties>
 <dependencies>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>${spring.version}</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-orm</artifactId>
   <version>${spring.version}</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aop</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <!--@Transactional -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-tx</artifactId>
   <version>${spring.version}</version>
  </dependency>

  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-entitymanager</artifactId>
   <version>${hibernate.version}</version>
  </dependency>
  <!-- 
  <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-search</artifactId>
   <version>4.1.1.Final</version>
  </dependency>
   -->
     <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-ehcache</artifactId>
       <version>${hibernate.version}</version>
     </dependency>
  <!-- connection pooling with c3p0 -->
  <dependency>
   <groupId>c3p0</groupId>
   <artifactId>c3p0</artifactId>
   <version>0.9.1.2</version>
  </dependency>

  <dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.17</version>
  </dependency>

  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.38</version>
  </dependency>

  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
  </dependency>

  <dependency>
   <groupId>cglib</groupId>
   <artifactId>cglib</artifactId>
   <version>3.1</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-test</artifactId>
   <version>${spring.version}</version>
   <scope>test</scope>
  </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.20</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.20</version>
    </dependency>
 </dependencies>
</project>


applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
 >
 <!-- Data Source -->
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/myssh"/>
  <property name="username" value="root"/>
    <property name="password" value="root"/>
    <property name="initialSize" value="3"/>
    <property name="maxActive" value="500"/>
    <property name="maxIdle" value="2"/>
    <property name="minIdle" value="1"/>
 </bean>
 
 <!--Spring Cache--><!-- 
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" />
            </set>
        </property>
    </bean>
-->    
 <!-- Local Session Factory Bean -->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="mappingResources">
   <list>
    <value>com/gvace/domain/Employee.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <value>
    hibernate.hbm2ddl.auto=update
    
    hibernate.dialect=org.hibernate.dialect.MySQLDialect
    hibernate.show_sql=true
    hibernate.format_sql=true
    
    hibernate.connection.CharSet=utf8
    hibernate.connection.characterEncoding=utf8
    hibernate.connection.useUnicode=true
    
    hibernate.generate_statistics=true
    hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
    hibernate.cache.use_structured_entries=true
    hibernate.cache.use_second_level_cache=true
    hibernate.cache.use_query_cache=true   
   </value>
  </property>
 </bean>
 <!-- Spring Transaction Manager -->
 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <!-- Start Transaction Annotation -->
 <tx:annotation-driven transaction-manager="transactionManager"/>
 
 <!-- Service Beans -->
 <bean id="employeeService" class="com.gvace.service.impl.EmployeeService">
  <property name="sessionFactory" ref="sessionFactory"/>
 </bean>
</beans>












































No comments:

Post a Comment