title: Spring Boot 2.0 Hibernate 5 EhCache 3 with JCache
body: <p>I'm trying to setup Hibernate with EhCache as the second level cache but the TTL is not working. </p>
  
  <p>Here are my dependencies:</p>
  
  <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-cache&lt;/artifactId&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
    &lt;artifactId&gt;hibernate-jcache&lt;/artifactId&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;org.ehcache&lt;/groupId&gt;
    &lt;artifactId&gt;ehcache&lt;/artifactId&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;javax.cache&lt;/groupId&gt;
    &lt;artifactId&gt;cache-api&lt;/artifactId&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Here's my YAML configuration:</p>
  
  <pre><code>spring:
    jpa:
      show-sql: true
      properties:
        hibernate:
          dialect: Dialect
          cache:
            use_second_level_cache: true
            region.factory_class: org.hibernate.cache.jcache.JCacheRegionFactory
            use_query_cache: true
    cache:
      jcache:
        config: classpath:ehcache.xml
  </code></pre>
  
  <p>Here's how my Entity class is configured:</p>
  
  <pre><code>@Entity
  @javax.persistence.Cacheable
  @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
  public class PersonEntity {
    //
  }
  </code></pre>
  
  <p>And the JpaRepository for the entity:</p>
  
  <pre><code>public interface PersonRepository extends JpaRepository&lt;PersonEntity, Integer&gt; {
    @org.springframework.data.jpa.repository.QueryHints({
        @javax.persistence.QueryHint(name = "org.hibernate.cacheable", value = "true")
    })
    List&lt;PersonEntity&gt; findByName(String name);
  }
  </code></pre>
  
  <p>I've configured the cache to expire in 2 seconds, but calling <code>findByName</code> still uses the cache (there are no SQL Queries printed after the first one).</p>
  
  <p>Here's the <code>ehcache.xml</code> file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;config xmlns="http://www.ehcache.org/v3"&gt;
  
    &lt;cache-template name="simple"&gt;
      &lt;expiry&gt;
        &lt;ttl&gt;2&lt;/ttl&gt;
      &lt;/expiry&gt;
      &lt;heap&gt;100&lt;/heap&gt;
    &lt;/cache-template&gt;
  
    &lt;cache alias="com.sample.PersonEntity" uses-template="simple"/&gt;
  
  &lt;/config&gt;
  </code></pre>
  
  <p><strong>EDIT:</strong>
  I've done some debugging. I've added a break point in <code>org.ehcache.jsr107.ExpiryPolicyToEhcacheExpiry</code>:</p>
  
  <pre><code>javax.cache.expiry.Duration duration = this.expiryPolicy.getExpiryForCreation();
  </code></pre>
  
  <p>This duration is INFINITE for some reason. So maybe the configuration is not set properly? I know the xml is being read because when I invalidate it (by removing heap tag for example) I get an error.</p>
  
  <p><a href="https://i.stack.imgur.com/Hxaez.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Hxaez.png" alt="enter image description here"></a></p>