title: EHCache with Spring 3.1 and Hibernate 4 annotations - making it works
body: <p>I'm trying to get EHCache working within my app. First thing I did was adding maven dependency:</p>
  
  <p><strong>pom.xml</strong></p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-ehcache&lt;/artifactId&gt;
          &lt;version&gt;${hibernate-version}&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>So far so good, now within my application root-context.xml (SessionFactory is defiend in roout because of <code>OpenSessionInView</code> filter) I added MBean for Hibernate statistics from jConsole and full definition of my sessionFactory:</p>
  
  <p><strong>root-context.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
  &lt;!-- Root Context: defines shared resources visible to all other web components --&gt;
  &lt;bean id="jmxExporter"
        class="org.springframework.jmx.export.MBeanExporter"&gt;
      &lt;property name="beans"&gt;
          &lt;map&gt;
              &lt;entry key="Hibernate:type=statistics"&gt;
                  &lt;ref local="statisticsBean"/&gt;
              &lt;/entry&gt;
          &lt;/map&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="statisticsBean" class="org.hibernate.jmx.StatisticsService"&gt;
      &lt;property name="statisticsEnabled" value="true"/&gt;
      &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="namingStrategy" class="com.execon.OracleNamingStrategy"/&gt;
  
  &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"&gt;
      &lt;property name="driverClass" value="oracle.jdbc.OracleDriver"/&gt;
      &lt;property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/&gt;
      &lt;property name="user" value="xxx"/&gt;
      &lt;property name="password" value="xxx"/&gt;
      &lt;property name="maxPoolSize" value="10"/&gt;
      &lt;property name="maxStatements" value="0"/&gt;
      &lt;property name="minPoolSize" value="5"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="namingStrategy" ref="namingStrategy"/&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="configLocation" value="classpath:hibernate.cfg.xml"/&gt;
      &lt;property name="packagesToScan" value="com.execon.models"/&gt;
  &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>Time to define hibernate.cfg.xml and ehcache file, so here they are:</p>
  
  <p><strong>hibernate.cfg.xml</strong></p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD//EN"
      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
  &lt;session-factory&gt;
      &lt;property name="show_sql"&gt;true&lt;/property&gt;
      &lt;property name="format_sql"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.cache.region.factory_class"&gt;org.hibernate.cache.ehcache.EhCacheRegionFactory&lt;/property&gt;
      &lt;property name="hibernate.cache.use_query_cache"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.cache.use_second_level_cache"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.cache.provider_configuration_file_resource_path"&gt;ehcache.xml&lt;/property&gt;
      &lt;property name="hibernate.generate_statistics"&gt;true&lt;/property&gt;
  &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p><strong>ehcache.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"&gt;
  
  &lt;diskStore path="java.io.tmpdir"/&gt;
  &lt;defaultCache
          eternal="false"
          maxElementsInMemory="1000"
          maxElementsOnDisk="10000"
          overflowToDisk="true"
          diskPersistent="true"
          timeToLiveSeconds="300"
          /&gt;
  &lt;/ehcache&gt;
  </code></pre>
  
  <p>Everything is working great, so now its time to define some <code>Service</code> to test cache, so I did:</p>
  
  <pre><code>@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&lt;SettlementModelGroup&gt; getModelGroups()
      {
          List&lt;SettlementModelGroup&gt; list = new ArrayList&lt;SettlementModelGroup&gt;();
          Session session = sessionFactory.getCurrentSession();
          Query query = session.createQuery( "from SettlementModelGroup" );
  
          list.addAll( query.list() );
  
          return list;
      }
  }
  </code></pre>
  
  <p>As you can see, this basic method alwas returns me same list. So I'm checking hibernate statistics and:</p>
  
  <pre><code>secondLevelCacheHitCount 0
  secondLevelCacheMissCount 0
  secondLevelCachePutCount 0
  </code></pre>
  
  <p>Rest of the statistics on screen:</p>
  
  <p><a href="https://i.stack.imgur.com/VgRDG.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VgRDG.jpg" alt="diagram"></a>
  Link if too small: <a href="http://s11.postimage.org/yfg9h6m83/image.jpg" rel="nofollow noreferrer">http://s11.postimage.org/yfg9h6m83/image.jpg</a></p>
  
  <p>So whats wrong, did I miss something (obvious)? Or am I going completly wrong way?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p><strong>SettlementModelGroup</strong> Entity (tried also CacheConcurrencyStrategy.READ_WRITE)</p>
  
  <pre><code>@Entity
  @Table(name = "MODEL_GROUP")
  @Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
  public class SettlementModelGroup implements Serializable
  {
  @Id
  @GeneratedValue(generator = "MODEL_GROUP_SEQ", strategy = GenerationType.SEQUENCE)
  @SequenceGenerator(name = "MODEL_GROUP_SEQ", sequenceName = "SEQ_MODEL_GROUP_MODEL_GROUP_ID")
  @Column(name = "MODEL_GROUP_ID", nullable = false)
  private Integer modelId;
  
  @Column(name = "NAME", nullable = false)
  private String modelGroupName;
  
  @Column(name = "DESCRIPTION", nullable = false)
  private String modelGroupDescription;
  
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "MODEL_GROUP_TYPE_ID", nullable = false)
  private SettlementModelGroupType settlementModelGroupType;
  
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "PERIOD_TYPE_ID", nullable = false)
  private PeriodType periodType;
  
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "DOMAIN_ID")
  private Domain domain;
  
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "OWNER_ID", nullable = false)
  private User user;
  
  @OneToMany(fetch = FetchType.LAZY, mappedBy = "modelId")
  @Cascade(CascadeType.ALL)
  private List&lt;SettlementModel&gt; settlementModels;
  
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "STATUS_ID")
  private Status status;
  
  //getters and setters here
  }
  </code></pre>