Java net.sf.ehcache.ObjectExistsException:已配置默认缓存

Java net.sf.ehcache.ObjectExistsException:已配置默认缓存,java,spring,hibernate,ehcache,database-caching,Java,Spring,Hibernate,Ehcache,Database Caching,我将Spring(4.2.6.RELEASE)与Hibernate(5.1.0.Final)结合使用。在spring.xml中定义为bean的Hibernate属性。 我为二级缓存库添加了ehcache。 我收到错误net.sf.ehcache.ObjectExistsException:默认缓存已在以下项目中配置。 有人帮忙吗 *****************************applicationContext-dao.xml*****************************

我将Spring(4.2.6.RELEASE)与Hibernate(5.1.0.Final)结合使用。在spring.xml中定义为bean的Hibernate属性。
我为二级缓存库添加了ehcache。 我收到错误net.sf.ehcache.ObjectExistsException:默认缓存已在以下项目中配置。
有人帮忙吗

*****************************applicationContext-dao.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="databaseProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="file:///C:/Config/database.properties"/>
    </bean>


    <bean id="transactionDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>

    <!-- Hibernate 4 SessionFactory Bean definition -->
    <bean id="hibernate4AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="transactionDatasource" />
        <property name="annotatedClasses">
            <list>
                <value>com.company.model.db.ApplicationStatEntity</value>
                <value>com.company.DeviceCapEntity</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.jdbc.batch_size">50</prop>
                <prop key="hibernate.connection.url">jdbc:oracle:thin:@<IP>:1522/rac2</prop>
                <prop key="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</prop>

                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                <prop key="org.hibernate.cache.ehcache.configurationResourceName">/ehcache.xml</prop>
                <!--property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property-->

            </props>
        </property>
    </bean>

</beans>
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.hibernate.*;
import org.hibernate.hql.internal.ast.QuerySyntaxException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Iterator;
import java.util.List;



public class GenericDaoImpl<GenericEntity> implements GenericDao<GenericEntity> {

    private Class<GenericEntity> type;
    private ClassPathXmlApplicationContext context = null;
    protected SessionFactory sessionFactory = null;
    Logger log = Logger.getLogger(GenericDaoImpl.class.getName());

    public GenericDaoImpl() {
        context = getApplicationContext();
        sessionFactory = getSessionFactory();
    }

    private ClassPathXmlApplicationContext getApplicationContext() {
        if (this.context == null) {
            this.context = new ClassPathXmlApplicationContext("applicationContext-dao.xml");
        }
        return this.context;
    }

    private SessionFactory getSessionFactory() {
        if(this.sessionFactory==null) {
            this.sessionFactory = (SessionFactory) getApplicationContext().getBean("hibernate4AnnotatedSessionFactory");
        }
        return this.sessionFactory;
    }

    public Class<GenericEntity> getMyType() {
        return this.type;
    }

    public void setType(Class<GenericEntity> type) {
        this.type = type;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }


    public void insert(GenericEntity genericEntity) throws DaoException {
        Session session = this.sessionFactory.openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            log.info("[GeneicDaoImpl.insert]  hibernate session opened");
            session.save(genericEntity);
            log.info("[GeneicDaoImpl.insert]  insertion done");
            transaction.commit();
        } catch (QuerySyntaxException e) {
            if (transaction != null) {
                transaction.rollback();
                log.error(
                        "[GeneicDaoImpl.insert]  An error occured ID generation with table mapping. "
                        + "Check schema,table name in entity object or check table in database",
                        e);
                throw new DaoException("[GeneicDaoImpl.insert]  hibernate error occured,rollback done. Stack Trace : /n" + e.getStackTrace());
            }
        } catch (AnnotationException e) {
            if (transaction != null) {
                transaction.rollback();
                log.error("[GeneicDaoImpl.insert]  An error occured ID generation with sequence. Check sequence name in entity object and in database",
                          e);
                throw new DaoException("[GeneicDaoImpl.insert]  hibernate error occured,rollback done. Stack Trace : /n" + e.getStackTrace());
            }
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
                log.error("[GeneicDaoImpl.insert]  hibernate error occured,rollback done", e);
                throw new DaoException("[GeneicDaoImpl.insert]  hibernate error occured,rollback done. Stack Trace : /n" + e.getStackTrace());
            }

        } finally {
            session.close();
        }
    }
}
    public class TestClass {

        @Test
        public void insertTest() {
            GenericDao<ApplicationStatEntity> insertBatchDao = new GenericDaoImpl();
            ApplicationStatEntity applicationStatEntity = new ApplicationStatEntity();
            applicationStatEntity.setId(102);
            applicationStatEntity.setDeviceid("SamsungShitty");
            try {
                insertBatchDao.insert(applicationStatEntity);
            } catch (DaoException e) {
                e.printStackTrace();
            }
        }
    }
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>

    <diskStore path="java.io.tmpdir/ehcache" />

    <defaultCache maxEntriesLocalHeap="10000" eternal="false"
                  timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30"
                  maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"
                  memoryStoreEvictionPolicy="LRU" statistics="true">
        <persistence strategy="localTempSwap" />
    </defaultCache>


</ehcache>

com.company.model.db.applicationstatity
com.company.DeviceCapEntity
org.hibernate.dialen.oracle10galent
线
假的
50
jdbc:oracle:thin:@:1522/rac2
oracle.jdbc.OracleDriver
真的
真的
org.hibernate.cache.ehcache.EhCacheRegionFactory
/ehcache.xml
我实现了以下GenericDao类。此类将实体作为泛型获取

*****************************通用刀**********************

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="databaseProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="file:///C:/Config/database.properties"/>
    </bean>


    <bean id="transactionDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>

    <!-- Hibernate 4 SessionFactory Bean definition -->
    <bean id="hibernate4AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="transactionDatasource" />
        <property name="annotatedClasses">
            <list>
                <value>com.company.model.db.ApplicationStatEntity</value>
                <value>com.company.DeviceCapEntity</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.jdbc.batch_size">50</prop>
                <prop key="hibernate.connection.url">jdbc:oracle:thin:@<IP>:1522/rac2</prop>
                <prop key="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</prop>

                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                <prop key="org.hibernate.cache.ehcache.configurationResourceName">/ehcache.xml</prop>
                <!--property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property-->

            </props>
        </property>
    </bean>

</beans>
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.hibernate.*;
import org.hibernate.hql.internal.ast.QuerySyntaxException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Iterator;
import java.util.List;



public class GenericDaoImpl<GenericEntity> implements GenericDao<GenericEntity> {

    private Class<GenericEntity> type;
    private ClassPathXmlApplicationContext context = null;
    protected SessionFactory sessionFactory = null;
    Logger log = Logger.getLogger(GenericDaoImpl.class.getName());

    public GenericDaoImpl() {
        context = getApplicationContext();
        sessionFactory = getSessionFactory();
    }

    private ClassPathXmlApplicationContext getApplicationContext() {
        if (this.context == null) {
            this.context = new ClassPathXmlApplicationContext("applicationContext-dao.xml");
        }
        return this.context;
    }

    private SessionFactory getSessionFactory() {
        if(this.sessionFactory==null) {
            this.sessionFactory = (SessionFactory) getApplicationContext().getBean("hibernate4AnnotatedSessionFactory");
        }
        return this.sessionFactory;
    }

    public Class<GenericEntity> getMyType() {
        return this.type;
    }

    public void setType(Class<GenericEntity> type) {
        this.type = type;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }


    public void insert(GenericEntity genericEntity) throws DaoException {
        Session session = this.sessionFactory.openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            log.info("[GeneicDaoImpl.insert]  hibernate session opened");
            session.save(genericEntity);
            log.info("[GeneicDaoImpl.insert]  insertion done");
            transaction.commit();
        } catch (QuerySyntaxException e) {
            if (transaction != null) {
                transaction.rollback();
                log.error(
                        "[GeneicDaoImpl.insert]  An error occured ID generation with table mapping. "
                        + "Check schema,table name in entity object or check table in database",
                        e);
                throw new DaoException("[GeneicDaoImpl.insert]  hibernate error occured,rollback done. Stack Trace : /n" + e.getStackTrace());
            }
        } catch (AnnotationException e) {
            if (transaction != null) {
                transaction.rollback();
                log.error("[GeneicDaoImpl.insert]  An error occured ID generation with sequence. Check sequence name in entity object and in database",
                          e);
                throw new DaoException("[GeneicDaoImpl.insert]  hibernate error occured,rollback done. Stack Trace : /n" + e.getStackTrace());
            }
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
                log.error("[GeneicDaoImpl.insert]  hibernate error occured,rollback done", e);
                throw new DaoException("[GeneicDaoImpl.insert]  hibernate error occured,rollback done. Stack Trace : /n" + e.getStackTrace());
            }

        } finally {
            session.close();
        }
    }
}
    public class TestClass {

        @Test
        public void insertTest() {
            GenericDao<ApplicationStatEntity> insertBatchDao = new GenericDaoImpl();
            ApplicationStatEntity applicationStatEntity = new ApplicationStatEntity();
            applicationStatEntity.setId(102);
            applicationStatEntity.setDeviceid("SamsungShitty");
            try {
                insertBatchDao.insert(applicationStatEntity);
            } catch (DaoException e) {
                e.printStackTrace();
            }
        }
    }
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>

    <diskStore path="java.io.tmpdir/ehcache" />

    <defaultCache maxEntriesLocalHeap="10000" eternal="false"
                  timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30"
                  maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"
                  memoryStoreEvictionPolicy="LRU" statistics="true">
        <persistence strategy="localTempSwap" />
    </defaultCache>


</ehcache>
import org.apache.commons.logging.Log;
导入org.apache.commons.logging.LogFactory;
导入org.apache.log4j.Logger;
导入org.hibernate.*;
导入org.hibernate.hql.internal.ast.QuerySyntaxException;
导入org.springframework.context.support.ClassPathXmlApplicationContext;
导入java.util.Iterator;
导入java.util.List;
公共类GenericDaoImpl实现GenericDao{
私人阶级类型;
私有类路径XmlApplicationContext上下文=null;
受保护的SessionFactory SessionFactory=null;
Logger log=Logger.getLogger(GenericDaoImpl.class.getName());
公共通用DAOImpl(){
context=getApplicationContext();
sessionFactory=getSessionFactory();
}
私有类路径XmlApplicationContext getApplicationContext(){
if(this.context==null){
this.context=new ClassPathXmlApplicationContext(“applicationContext dao.xml”);
}
返回这个.context;
}
私有SessionFactory getSessionFactory(){
if(this.sessionFactory==null){
this.sessionFactory=(sessionFactory)getApplicationContext().getBean(“hibernate4AnnotatedSessionFactory”);
}
返回此.sessionFactory;
}
公共类getMyType(){
返回此.type;
}
公共void集合类型(类类型){
this.type=type;
}
public void setSessionFactory(SessionFactory SessionFactory){
this.sessionFactory=sessionFactory;
}
公共void insert(GenericeEntity GenericeEntity)引发异常{
Session Session=this.sessionFactory.openSession();
事务=空;
试一试{
事务=session.beginTransaction();
log.info(“[GeneicDaoImpl.insert]hibernate会话已打开”);
session.save(一般实体);
log.info(“[GeneicDaoImpl.insert]插入完成”);
commit();
}捕获(QuerySyntaxException e){
if(事务!=null){
transaction.rollback();
日志错误(
“[GeneicDaoImpl.insert]使用表映射生成ID时出错。”
+“检查架构、实体对象中的表名或数据库中的检查表”,
e) );
抛出新的DaoException(“[GeneicDaoImpl.insert]发生hibernate错误,回滚完成。堆栈跟踪:/n”+e.getStackTrace());
}
}捕获(注释异常e){
if(事务!=null){
transaction.rollback();
log.error(“[GeneicDaoImpl.insert]使用序列生成ID时出错。请检查实体对象和数据库中的序列名称”,
e) );
抛出新的DaoException(“[GeneicDaoImpl.insert]发生hibernate错误,回滚完成。堆栈跟踪:/n”+e.getStackTrace());
}
}捕获(例外e){
if(事务!=null){
transaction.rollback();
log.error(“[GeneicDaoImpl.insert]发生休眠错误,回滚完成”,e);
抛出新的DaoException(“[GeneicDaoImpl.insert]发生hibernate错误,回滚完成。堆栈跟踪:/n”+e.getStackTrace());
}
}最后{
session.close();
}
}
}
在Junit测试类中,我像这样调用insert批处理

**************************TestClass.java*****************************

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="databaseProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="file:///C:/Config/database.properties"/>
    </bean>


    <bean id="transactionDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>

    <!-- Hibernate 4 SessionFactory Bean definition -->
    <bean id="hibernate4AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="transactionDatasource" />
        <property name="annotatedClasses">
            <list>
                <value>com.company.model.db.ApplicationStatEntity</value>
                <value>com.company.DeviceCapEntity</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.jdbc.batch_size">50</prop>
                <prop key="hibernate.connection.url">jdbc:oracle:thin:@<IP>:1522/rac2</prop>
                <prop key="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</prop>

                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                <prop key="org.hibernate.cache.ehcache.configurationResourceName">/ehcache.xml</prop>
                <!--property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property-->

            </props>
        </property>
    </bean>

</beans>
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.hibernate.*;
import org.hibernate.hql.internal.ast.QuerySyntaxException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Iterator;
import java.util.List;



public class GenericDaoImpl<GenericEntity> implements GenericDao<GenericEntity> {

    private Class<GenericEntity> type;
    private ClassPathXmlApplicationContext context = null;
    protected SessionFactory sessionFactory = null;
    Logger log = Logger.getLogger(GenericDaoImpl.class.getName());

    public GenericDaoImpl() {
        context = getApplicationContext();
        sessionFactory = getSessionFactory();
    }

    private ClassPathXmlApplicationContext getApplicationContext() {
        if (this.context == null) {
            this.context = new ClassPathXmlApplicationContext("applicationContext-dao.xml");
        }
        return this.context;
    }

    private SessionFactory getSessionFactory() {
        if(this.sessionFactory==null) {
            this.sessionFactory = (SessionFactory) getApplicationContext().getBean("hibernate4AnnotatedSessionFactory");
        }
        return this.sessionFactory;
    }

    public Class<GenericEntity> getMyType() {
        return this.type;
    }

    public void setType(Class<GenericEntity> type) {
        this.type = type;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }


    public void insert(GenericEntity genericEntity) throws DaoException {
        Session session = this.sessionFactory.openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            log.info("[GeneicDaoImpl.insert]  hibernate session opened");
            session.save(genericEntity);
            log.info("[GeneicDaoImpl.insert]  insertion done");
            transaction.commit();
        } catch (QuerySyntaxException e) {
            if (transaction != null) {
                transaction.rollback();
                log.error(
                        "[GeneicDaoImpl.insert]  An error occured ID generation with table mapping. "
                        + "Check schema,table name in entity object or check table in database",
                        e);
                throw new DaoException("[GeneicDaoImpl.insert]  hibernate error occured,rollback done. Stack Trace : /n" + e.getStackTrace());
            }
        } catch (AnnotationException e) {
            if (transaction != null) {
                transaction.rollback();
                log.error("[GeneicDaoImpl.insert]  An error occured ID generation with sequence. Check sequence name in entity object and in database",
                          e);
                throw new DaoException("[GeneicDaoImpl.insert]  hibernate error occured,rollback done. Stack Trace : /n" + e.getStackTrace());
            }
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
                log.error("[GeneicDaoImpl.insert]  hibernate error occured,rollback done", e);
                throw new DaoException("[GeneicDaoImpl.insert]  hibernate error occured,rollback done. Stack Trace : /n" + e.getStackTrace());
            }

        } finally {
            session.close();
        }
    }
}
    public class TestClass {

        @Test
        public void insertTest() {
            GenericDao<ApplicationStatEntity> insertBatchDao = new GenericDaoImpl();
            ApplicationStatEntity applicationStatEntity = new ApplicationStatEntity();
            applicationStatEntity.setId(102);
            applicationStatEntity.setDeviceid("SamsungShitty");
            try {
                insertBatchDao.insert(applicationStatEntity);
            } catch (DaoException e) {
                e.printStackTrace();
            }
        }
    }
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>

    <diskStore path="java.io.tmpdir/ehcache" />

    <defaultCache maxEntriesLocalHeap="10000" eternal="false"
                  timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30"
                  maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"
                  memoryStoreEvictionPolicy="LRU" statistics="true">
        <persistence strategy="localTempSwap" />
    </defaultCache>


</ehcache>
公共类TestClass{
@试验
公共void insertTest(){
GenericDao insertBatchDao=新的GenericDaoImpl();
ApplicationStatEntity ApplicationStatEntity=新的ApplicationStatEntity();
applicationStatEntity.setId(102);
applicationStatEntity.setDeviceid(“SamsungShitty”);
试一试{
insertBatchDao.insert(应用程序状态);
}捕获(DAOE){
e、 printStackTrace();
}
}
}
*****************************ehcache.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="databaseProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="file:///C:/Config/database.properties"/>
    </bean>


    <bean id="transactionDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>

    <!-- Hibernate 4 SessionFactory Bean definition -->
    <bean id="hibernate4AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="transactionDatasource" />
        <property name="annotatedClasses">
            <list>
                <value>com.company.model.db.ApplicationStatEntity</value>
                <value>com.company.DeviceCapEntity</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.jdbc.batch_size">50</prop>
                <prop key="hibernate.connection.url">jdbc:oracle:thin:@<IP>:1522/rac2</prop>
                <prop key="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</prop>

                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                <prop key="org.hibernate.cache.ehcache.configurationResourceName">/ehcache.xml</prop>
                <!--property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property-->

            </props>
        </property>
    </bean>

</beans>
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.hibernate.*;
import org.hibernate.hql.internal.ast.QuerySyntaxException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Iterator;
import java.util.List;



public class GenericDaoImpl<GenericEntity> implements GenericDao<GenericEntity> {

    private Class<GenericEntity> type;
    private ClassPathXmlApplicationContext context = null;
    protected SessionFactory sessionFactory = null;
    Logger log = Logger.getLogger(GenericDaoImpl.class.getName());

    public GenericDaoImpl() {
        context = getApplicationContext();
        sessionFactory = getSessionFactory();
    }

    private ClassPathXmlApplicationContext getApplicationContext() {
        if (this.context == null) {
            this.context = new ClassPathXmlApplicationContext("applicationContext-dao.xml");
        }
        return this.context;
    }

    private SessionFactory getSessionFactory() {
        if(this.sessionFactory==null) {
            this.sessionFactory = (SessionFactory) getApplicationContext().getBean("hibernate4AnnotatedSessionFactory");
        }
        return this.sessionFactory;
    }

    public Class<GenericEntity> getMyType() {
        return this.type;
    }

    public void setType(Class<GenericEntity> type) {
        this.type = type;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }


    public void insert(GenericEntity genericEntity) throws DaoException {
        Session session = this.sessionFactory.openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            log.info("[GeneicDaoImpl.insert]  hibernate session opened");
            session.save(genericEntity);
            log.info("[GeneicDaoImpl.insert]  insertion done");
            transaction.commit();
        } catch (QuerySyntaxException e) {
            if (transaction != null) {
                transaction.rollback();
                log.error(
                        "[GeneicDaoImpl.insert]  An error occured ID generation with table mapping. "
                        + "Check schema,table name in entity object or check table in database",
                        e);
                throw new DaoException("[GeneicDaoImpl.insert]  hibernate error occured,rollback done. Stack Trace : /n" + e.getStackTrace());
            }
        } catch (AnnotationException e) {
            if (transaction != null) {
                transaction.rollback();
                log.error("[GeneicDaoImpl.insert]  An error occured ID generation with sequence. Check sequence name in entity object and in database",
                          e);
                throw new DaoException("[GeneicDaoImpl.insert]  hibernate error occured,rollback done. Stack Trace : /n" + e.getStackTrace());
            }
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
                log.error("[GeneicDaoImpl.insert]  hibernate error occured,rollback done", e);
                throw new DaoException("[GeneicDaoImpl.insert]  hibernate error occured,rollback done. Stack Trace : /n" + e.getStackTrace());
            }

        } finally {
            session.close();
        }
    }
}
    public class TestClass {

        @Test
        public void insertTest() {
            GenericDao<ApplicationStatEntity> insertBatchDao = new GenericDaoImpl();
            ApplicationStatEntity applicationStatEntity = new ApplicationStatEntity();
            applicationStatEntity.setId(102);
            applicationStatEntity.setDeviceid("SamsungShitty");
            try {
                insertBatchDao.insert(applicationStatEntity);
            } catch (DaoException e) {
                e.printStackTrace();
            }
        }
    }
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>

    <diskStore path="java.io.tmpdir/ehcache" />

    <defaultCache maxEntriesLocalHeap="10000" eternal="false"
                  timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30"
                  maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"
                  memoryStoreEvictionPolicy="LRU" statistics="true">
        <persistence strategy="localTempSwap" />
    </defaultCache>


</ehcache>

使用cache标记而不是defaultCache

ehcache.xml可以是

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <diskStore path="java.io.tmpdir/ehcache" />

    <defaultCache maxEntriesLocalHeap="10000" eternal="false"
                  timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30"
                  maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"
                  memoryStoreEvictionPolicy="LRU" statistics="true">
        <persistence strategy="localTempSwap"/>
    </defaultCache>



    <cache name="sampleCache" 
        maxEntriesLocalHeap="10000" 
        eternal="false"
        timeToIdleSeconds="120" 
        timeToLiveSeconds="120" 
        diskSpoolBufferSizeMB="30"
        maxEntriesLocalDisk="10000000" 
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU" 
        statistics="true">
        <persistence strategy="localTempSwap" />
    </cache>

</ehcache>

来自ehcache文档

<!--

Default Cache configuration. These settings will be applied to caches
created programmatically using CacheManager.add(String cacheName).
This element is optional, and using CacheManager.add(String cacheName) when
its not present will throw CacheException

The defaultCache has an implicit name "default" which is a reserved cache name.

-->

<defaultCache maxEntriesLocalHeap="0" eternal="false" timeToIdleSeconds="1200" timeToLiveSeconds="1200">
    <terracotta/>
</defaultCache>