Question

My project uses Spring 3.x, Hibernate 4.2.7, JPA and Maven. It was migrated from a 3.x Hibernate version. Previously, the project used the following package:

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.3.1</version>
</dependency>

I found out that Hibernate has its own implementation from EhCache, so I updated my package configuration like this:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.2.7.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.2.7.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>4.2.7.Final</version>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <version>4.2.7.Final</version>
</dependency>

Worked fine, but now I have a problem: how to specify my ehcache.xml previous declaration? My actual persistence.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">

<persistence-unit name="consultlog">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="validate" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" />
        <property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property>
        <property name="hibernate.cache.use_second_level_cache" value="true" />
        <property name="hibernate.cache.use_query_cache" value="true" />
        <property name="default_batch_fetch_size" value="5" />
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
        <property name="hibernate.connection.url" value="jdbc:postgresql://localhost/mydatabase" />
        <property name="hibernate.connection.username" value="myuser" />
        <property name="hibernate.connection.password" value="mypassword" />

        <property name="hibernate.connection.provider_class"
          value="org.hibernate.connection.C3P0ConnectionProvider" />

        <property name="hibernate.c3p0.max_size" value="80" />
        <property name="hibernate.c3p0.min_size" value="0" />
        <property name="hibernate.c3p0.acquire_increment" value="1" />
        <property name="hibernate.c3p0.idle_test_period" value="300" />
        <property name="hibernate.c3p0.max_statements" value="0" />
        <property name="hibernate.c3p0.timeout" value="100" />
    </properties>
</persistence-unit>

How I have to replace this line?

<property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property>

Or even better, how I can use persistence.xml to perform the configuration?

Was it helpful?

Solution

Looking at the source code for SingletonEhCacheRegionFactory and AbstractEhcacheRegionFactory which are responsible for loading the ehcache.xml, the name of the property is still called net.sf.ehcache.configurationResourceName (I'm looking at version 4.3 of hibernate-ehcache).

In addition, if the file is called ehcache.xml and lives in the root of the classpath, then there is no need to specify the property as that filename will be loaded by default. You only need to specify the property if you want a different name - such as ehcache-2.xml - or if the file does not live in the root of the classpath.

I think you should be safe to leave your property the way it is - or perhaps remove it completely if your ehcache.xml lives in the root of the classpath (which it appears to do from looking at your config).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top