title: Why doesn't Hibernate L2 cache turn on?
body: <p>I do not understand why my Hibernate L2 cache doesn't work.
  I have Spring Boot application with following configuration</p>
  <pre><code>  jpa:
      database-platform: org.hibernate.dialect.PostgreSQL10Dialect
      show-sql: true
      properties:
        hibernate:
          format_sql: true
          cache :
            use_second_level_cache: true
            region :
              factory_class: org.hibernate.cache.ehcache.EhCacheRegionFactory
            use_query_cache : true
            provider_class : net.sf.ehcache.hibernate.SingletonEhCacheProvider
            generate_statistics : true
            use_structured_entries : true
  </code></pre>
  <p>Following entity mush be cached</p>
  <pre><code>@Entity
  @Table(name = &quot;employees&quot;, schema = &quot;security&quot;, catalog = &quot;&quot;)
  //@SQLDelete(sql = &quot;update security.employees set deleted_ts = current_timestamp where id=?&quot;)
  //@Where(clause = &quot;deleted_ts is null&quot;)
  @Cacheable
  @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
  class Employee : BaseUuidEntity(), CompanyRelated,UserRelated{
  
      @ManyToOne
      @JoinColumn(name = &quot;user_id&quot;, nullable = false)
      var user: User? = null
  
      @ManyToOne
      @JoinColumn(name = &quot;company_id&quot;)
      var company: Company? = null
  
      @Column(name = &quot;owner&quot;)
      var owner: Boolean? = null
  
      @ManyToOne
      @JoinColumn(name = &quot;role_id&quot;)
      var role: Role? = null
  
      @ElementCollection
      @CollectionTable(
          name = &quot;employee_project&quot;,
          schema = &quot;security&quot;,
          joinColumns = [JoinColumn(name = &quot;employee_id&quot;)]
      )
      @Column(name = &quot;project_id&quot;)
      var projectIds: MutableSet&lt;UUID&gt;? = null
  
  
      override fun getObjUserId(): UUID? {
          return user?.id
      }
  
      override fun getObjCompanyId(): UUID? {
          return company?.id
      }
  
  }
  </code></pre>
  <p>But in logs in second page request I have repeated query:</p>
  <pre><code>Hibernate: 
      select
          employee0_.id as id1_37_,
          employee0_.created_by as created_2_37_,
          employee0_.created_ts as created_3_37_,
          employee0_.deleted_by as deleted_4_37_,
          employee0_.deleted_ts as deleted_5_37_,
          employee0_.updated_by as updated_6_37_,
          employee0_.updated_ts as updated_7_37_,
          employee0_.company_id as company_9_37_,
          employee0_.owner as owner8_37_,
          employee0_.role_id as role_id10_37_,
          employee0_.user_id as user_id11_37_ 
      from
          security.employees employee0_ 
      left outer join
          security.employee_project projectids1_ 
              on employee0_.id=projectids1_.employee_id 
      where
          projectids1_.project_id=?
  </code></pre>
  <p>The queries are called this way</p>
  <pre><code>...
  employeeRepository.findByProjectIds(project.id!!)
  ...
  interface EmployeeRepository : JpaSpecRepository&lt;Employee&gt; {
  ...
      @PostFilter(&quot;hasObjectPermission(filterObject, 'Employee', 'View')&quot;)
      fun findByProjectIds(projectId: UUID): MutableList&lt;Employee&gt;
  ...
  </code></pre>
  <p>You can see full log here <a href="https://gist.github.com/iva-nova-e-katerina/37ef0b489cc94d693c6e7640ff01f507" rel="nofollow noreferrer">https://gist.github.com/iva-nova-e-katerina/37ef0b489cc94d693c6e7640ff01f507</a>
  Please teach me how to turn on Hibernate L2 cache?
  Probably a bit more TRACE level logs make situation clean:</p>
  <pre><code>2022-07-31 10:41:01,085 TRACE [http-nio-8080-exec-8] org.hibernate.type.descriptor.sql.BasicBinder: binding parameter [1] as [OTHER] - [dcc5ff7b-ec8b-45d7-be0f-bcf6a55f1206]
  2022-07-31 10:41:01,086 DEBUG [http-nio-8080-exec-8] org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler: Filtering with expression: hasObjectPermission(filterObject, 'Employee', 'View')
  2022-07-31 10:41:01,087 DEBUG [http-nio-8080-exec-8] org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler: Filtering collection with 0 elements
  2022-07-31 10:41:01,087 DEBUG [http-nio-8080-exec-8] org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler: Retaining elements: []
  2022-07-31 10:41:01,088 DEBUG [http-nio-8080-exec-8] org.hibernate.engine.jdbc.spi.SqlStatementLogger: 
      select
          employee0_.id as id1_37_,
          employee0_.created_by as created_2_37_,
  </code></pre>