title: JPA + Hibernate + EHCache, unexpected behavior
body: <p>I'm trying to implement EHCache into my working prototype, where I have a <em>javax.persistence.Entity</em> representing a table on my database (MySQL, mysql-connector-java-5.1.20.jar), which be provided as XML to the consumers.</p>
  
  <p>The issue I'm facing is that seems Hibernate still retrieving the data from the database, <strong>even when EHCache stores the query result on memory</strong>.</p>
  
  <p>I'm using the <a href="http://ehcache.org/documentation/operations/monitor#api" rel="nofollow">EHCache monitor</a> to see the count of items in memory, and changing the data directly on database before the cache expires to know if the cached data is actually used.</p>
  
  <p>I been looking for a replication for this issue without succeed, so maybe I'm missing something (I just geting into the java world).</p>
  
  <h2>My files</h2>
  
  <p><strong>pom.xml</strong></p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
      &lt;version&gt;4.3.0.Final&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
      &lt;version&gt;4.1.4.Final&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-ehcache&lt;/artifactId&gt;
      &lt;version&gt;4.1.4.Final&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;net.sf.ehcache&lt;/groupId&gt;
      &lt;artifactId&gt;ehcache-core&lt;/artifactId&gt;
      &lt;version&gt;2.5.2&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.terracotta&lt;/groupId&gt;
      &lt;artifactId&gt;ehcache-probe&lt;/artifactId&gt;
      &lt;version&gt;1.0.2&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p><strong>Entity class</strong></p>
  
  <pre><code>package myPrototype.entities
  
  import javax.persistence.Cacheable;
  import javax.persistence.Entity;
  import javax.persistence.NamedQueries;
  import javax.persistence.NamedQuery;
  import javax.persistence.Table;
  import javax.xml.bind.annotation.XmlRootElement;
  import org.hibernate.annotations.Cache;
  import org.hibernate.annotations.CacheConcurrencyStrategy;
  
  @Entity @Cacheable
  @Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
  @Table(name = "Cars")
  @XmlRootElement
  @NamedQueries({
      @NamedQuery(name = "Cars.findAll", query = "SELECT d FROM Cars d"),
      [...]
  public class Cars implements Serializable {
      private static final long serialVersionUID = 1L;
      @Basic(optional = false)
          @NotNull
          @Column(name = "id")
      @ReferenceField
      private int id;
      [...]
  </code></pre>
  
  <p><strong>ehcache.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ehcache&gt;
    &lt;cache name="myPrototype.entities.Cars"
      maxElementsInMemory="500"
      eternal="false"
      overflowToDisk="false"
      timeToIdleSeconds="60"
      timeToLiveSeconds="120" /&gt;
  &lt;/ehcache&gt;
  </code></pre>
  
  <p>* <em>I tried setting eternal="true", but the result still the same</em></p>
  
  <p><strong>persistence.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0" 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_2_0.xsd"&gt;
    &lt;persistence-unit name="MyPrototype" transaction-type="JTA"&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;jta-data-source&gt;java:app/jdbc/mysqlserver/prototype&lt;/jta-data-source&gt;
      &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
      &lt;shared-cache-mode&gt;ENABLE_SELECTIVE&lt;/shared-cache-mode&gt;
      &lt;properties&gt;
        &lt;!-- property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/ --&gt;
        &lt;property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" /&gt;
        &lt;property name="hibernate.cache.use_second_level_cache" value="true"/&gt;
        &lt;property name="hibernate.cache.use_query_cache" value="true"/&gt;
        &lt;property name="hibernate.cache.region_prefix" value=""/&gt;
      &lt;/properties&gt;
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>* <em>I tried switching between SingletonEhCacheRegionFactory and EhCacheRegionFactory</em></p>
  
  <p><strong>EJB</strong></p>
  
  <pre><code>import myPrototype.entities.Cars
  import java.util.Date;
  import java.util.List;
  import javax.ejb.Stateless;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  
  @Stateless
  public class CarsEJB {
  
      @PersistenceContext(unitName="MyPrototype")
      private EntityManager em;
  
      @Override
      public List&lt;Cars&gt; getCars() {
          return (List&lt;Cars&gt;)em.createNamedQuery("Cars.findAll").setMaxResults(200).getResultList();
      }
  
  }
  </code></pre>
  
  <p>* <em>Added to clarify how I run the query</em></p>
  
  <p>Like I said, I can see a count of items in memory through EHCache monitor, so I noted the following:
  - The ehcache configuration is property loaded elements are expiring
  - The elements expiration not being reseted when the query runs while they still alive (they expired N seconds after the first query).</p>
  
  <p>Again; I'm quite a newbie with java, so asume I could be missing basic concepts.</p>
  
  <p>Thanks for reading!</p>