EHCache with Spring 3.1 and Hibernate 4 annotations - making it works
===

I'm trying to get EHCache working within my app. First thing I did was adding maven dependency:

**pom.xml**

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>${hibernate-version}</version>
        </dependency>

So far so good, now within my application root-context.xml (SessionFactory is defiend in roout because of `OpenSessionInView` filter) I added MBean for Hibernate statistics from jConsole and full definition of my sessionFactory:

**root-context.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-3.0.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->
    <bean id="jmxExporter"
          class="org.springframework.jmx.export.MBeanExporter">
        <property name="beans">
            <map>
                <entry key="Hibernate:type=statistics">
                    <ref local="statisticsBean"/>
                </entry>
            </map>
        </property>
    </bean>
    <bean id="statisticsBean" class="org.hibernate.jmx.StatisticsService">
        <property name="statisticsEnabled" value="true"/>
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="namingStrategy" class="com.execon.OracleNamingStrategy"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="oracle.jdbc.OracleDriver"/>
        <property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
        <property name="user" value="xxx"/>
        <property name="password" value="xxx"/>
        <property name="maxPoolSize" value="10"/>
        <property name="maxStatements" value="0"/>
        <property name="minPoolSize" value="5"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="namingStrategy" ref="namingStrategy"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        <property name="packagesToScan" value="com.execon.models"/>
    </bean>
    </beans>

Time to define hibernate.cfg.xml and ehcache file, so here they are:

**hibernate.cfg.xml**

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

    <hibernate-configuration>
    <session-factory>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
        <property name="hibernate.cache.use_query_cache">true</property>
        <property name="hibernate.cache.use_second_level_cache">true</property>
        <property name="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</property>
        <property name="hibernate.generate_statistics">true</property>
    </session-factory>
    </hibernate-configuration>

**ehcache.xml**

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

    <diskStore path="java.io.tmpdir"/>
    <defaultCache
            eternal="false"
            maxElementsInMemory="1000"
            maxElementsOnDisk="10000"
            overflowToDisk="true"
            diskPersistent="true"
            timeToLiveSeconds="300"
            />
    </ehcache>

Everything is working great, so now its time to define some `Service` to test cache, so I did:

    @Service
    @Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
    @Transactional(readOnly = true)
    public class MyService
    {
        @Autowired
        private SessionFactory sessionFactory;

        @SuppressWarnings("unchecked")
        @Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
        public List<SettlementModelGroup> getModelGroups()
        {
            List<SettlementModelGroup> list = new ArrayList<SettlementModelGroup>();
            Session session = sessionFactory.getCurrentSession();
            Query query = session.createQuery( "from SettlementModelGroup" );

            list.addAll( query.list() );

            return list;
        }
    }

As you can see, this basic method alwas returns me same list. So I'm checking hibernate statistics and:

    secondLevelCacheHitCount 0
    secondLevelCacheMissCount 0
    secondLevelCachePutCount 0

Rest of the statistics on screen:

![diagram](http://s11.postimage.org/yfg9h6m83/image.jpg)
Link if too small: http://s11.postimage.org/yfg9h6m83/image.jpg

So whats wrong, did I miss something (obvious)? Or am I going completly wrong way?