title: Ehcache: java type cannot be cast to entity
body: <p>I have project where I use spring caching with Ehcache technology. Also I use hibernate as jpa provider.
  My request method invoke create several thread for data processing.</p>
  
  <p>It works well when I use different caches for every method of my service module in ehcache.xml like this:</p>
  
  <pre><code>&lt;cache name="rrr1"
     maxEntriesLocalHeap="10000"
     maxEntriesLocalDisk="100000000"
     eternal="false"
     diskSpoolBufferSizeMB="20"
     timeToIdleSeconds="300"
     timeToLiveSeconds="600"
     memoryStoreEvictionPolicy="LFU"
     overflowToDisk="false"
     diskPersistent="false"
     transactionalMode="off"&gt;
  &lt;/cache&gt;
  
  &lt;cache name="rrr2"
     maxEntriesLocalHeap="10000"
     maxEntriesLocalDisk="100000000"
     eternal="false"
     diskSpoolBufferSizeMB="20"
     timeToIdleSeconds="300"
     timeToLiveSeconds="600"
     memoryStoreEvictionPolicy="LFU"
     overflowToDisk="false"
     diskPersistent="false"
     transactionalMode="off"&gt;
  &lt;/cache&gt;
  
  &lt;cache name="rrr3"
     maxEntriesLocalHeap="10000"
     maxEntriesLocalDisk="100000000"
     eternal="false"
     diskSpoolBufferSizeMB="20"
     timeToIdleSeconds="300"
     timeToLiveSeconds="600"
     memoryStoreEvictionPolicy="LFU"
     overflowToDisk="false"
     diskPersistent="false"
     transactionalMode="off"&gt;
  &lt;/cache&gt;
  </code></pre>
  
  <p>But when I try to set single cache for my methods, I get issue like this:</p>
  
  <pre><code>2016-08-05 11:12:11.385  INFO 46536 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.1.0.Final}
  2016-08-05 11:12:11.387  INFO 46536 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
  2016-08-05 11:12:11.388  INFO 46536 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
  2016-08-05 11:12:11.426  INFO 46536 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
  2016-08-05 11:12:11.723  INFO 46536 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
  2016-08-05 11:12:12.110  WARN 46536 --- [           main] o.h.b.i.SessionFactoryBuilderImpl        : Unrecognized hbm2ddl_auto value : none.  Supported values include create, create-drop, update, and validate.  Ignoring
  2016-08-05 11:12:12.667  INFO 46536 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
  2016-08-05 11:12:12.910  INFO 46536 --- [           main] o.s.c.ehcache.EhCacheManagerFactoryBean  : Initializing EhCache CacheManager
  2016-08-05 11:12:12.941  WARN 46536 --- [           main] n.sf.ehcache.config.CacheConfiguration   : Statistics can no longer be enabled via configuration.
  2016-08-05 11:12:13.560  INFO 46536 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@78691363: startup date [Fri Aug 05 11:12:08 NOVT 2016]; root of context hierarchy
  2016-08-05 11:12:13.664  INFO 46536 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/greeting]}" onto public com.example.Greeting com.example.GreetingController.greeting(java.lang.String)
  2016-08-05 11:12:13.666  INFO 46536 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity&lt;java.util.Map&lt;java.lang.String, java.lang.Object&gt;&gt; org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
  2016-08-05 11:12:13.667  INFO 46536 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
  2016-08-05 11:12:13.715  INFO 46536 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  2016-08-05 11:12:13.715  INFO 46536 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  2016-08-05 11:12:13.772  INFO 46536 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  2016-08-05 11:12:14.379  INFO 46536 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
  2016-08-05 11:12:14.457  INFO 46536 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
  2016-08-05 11:12:14.462  INFO 46536 --- [           main] com.example.DemoApplication              : Started DemoApplication in 6.507 seconds (JVM running for 7.062)
  2016-08-05 11:12:32.666  INFO 46536 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
  2016-08-05 11:12:32.666  INFO 46536 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
  2016-08-05 11:12:32.685  INFO 46536 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms
  2016-08-05 11:12:33.162  INFO 46536 --- [       Thread-6] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
  ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
  ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
  ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
  ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
  ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
  ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
  ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
  ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
  ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.Standart
  ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.model.bs.Org
  ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.model.bs.Org
  ChrgServ: Error in thread:: java.lang.Boolean cannot be cast to com.ric.bill.model.bs.Org
  </code></pre>
  
  <p>Where com.ric.bill.Standart and com.ric.bill.model.bs.Org - my Entities</p>
  
  <p>Why does it happen?</p>
  
  <p>My ehcache.xml (not working):</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"
           updateCheck="false"&gt;
      &lt;diskStore path="cacheIO" /&gt;
  
      &lt;defaultCache maxElementsInMemory="1000000" eternal="true"
                    overflowToDisk="false" diskPersistent="false"
                    timeToIdleSeconds="0" timeToLiveSeconds="0"
                    diskExpiryThreadIntervalSeconds="1800" memoryStoreEvictionPolicy="FIFO"&gt;
      &lt;/defaultCache&gt;
  
      &lt;cache name="org.hibernate.cache.internal.StandardQueryCache"
          maxElementsInMemory="1000000" eternal="true" timeToLiveSeconds="0"
          overflowToDisk="false" /&gt;
      &lt;cache
  
      name="org.hibernate.cache.UpdateTimestampsCache"
      maxEntriesLocalHeap="5000"
      eternal="true"/&gt;
  
      &lt;cache name="rrr1" 
          maxEntriesLocalHeap="1000000"
          maxEntriesLocalDisk="1000000" 
          eternal="true" 
          diskPersistent="false"
          diskSpoolBufferSizeMB="100"
          timeToIdleSeconds="0" timeToLiveSeconds="0"
          memoryStoreEvictionPolicy="LFU" 
          overflowToDisk="false"
          transactionalMode="off"&gt;
      &lt;/cache&gt;
  
      &lt;cache name="rrr2" 
          maxEntriesLocalHeap="1000000"
          maxEntriesLocalDisk="1000000" 
          eternal="true" 
          diskPersistent="false"
          diskSpoolBufferSizeMB="100"
          timeToIdleSeconds="0" timeToLiveSeconds="0"
          memoryStoreEvictionPolicy="LFU"
          overflowToDisk="false" 
          transactionalMode="off"&gt;
      &lt;/cache&gt;
  
      &lt;cache name="rrr3" 
          maxEntriesLocalHeap="1000000"
          maxEntriesLocalDisk="1000000" 
          eternal="true" 
          diskPersistent="false"
          diskSpoolBufferSizeMB="100"
          timeToIdleSeconds="0" timeToLiveSeconds="0"
          memoryStoreEvictionPolicy="LFU"
          overflowToDisk="false" 
          transactionalMode="off"&gt;
      &lt;/cache&gt;
  
  &lt;/ehcache&gt;
  </code></pre>
  
  <p>My methods:</p>
  
  <pre><code>package com.ric.bill.mm.impl;
  
  import java.util.Date;
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.cache.annotation.Cacheable;
  import org.springframework.context.annotation.Scope;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import com.ric.bill.Calc;
  import com.ric.bill.Storable;
  import com.ric.bill.Utl;
  import com.ric.bill.dao.ParDAO;
  import com.ric.bill.excp.WrongGetMethod;
  import com.ric.bill.mm.ParMng;
  import com.ric.bill.model.bs.Dw;
  import com.ric.bill.model.bs.Par;
  
  @Service
  public class ParMngImpl implements ParMng {
  
      @Autowired
      private ParDAO pDao;
  
      @Cacheable(cacheNames="rrr1", key="{ #cd }")
      public synchronized boolean isExByCd(String cd) {
          Par p = getByCD(cd);
          if (p != null) {
              return true;
          } else {
              return false;
          }
      }
  
      @Cacheable(cacheNames="rrr1", key="{ #st.getKlsk(), #cd, #genDt }")
      public synchronized Double getDbl(Storable st, String cd, Date genDt) {
          Par par = getByCD(cd);
          try {
              for (Dw d: st.getDw()) {
                  //по соотв.периоду
                  if (Utl.between(genDt, d.getDt1(), d.getDt2())) {
                      //проверка, что соответствует CD и Number (NM), Единичное (SI) - убрал - тормозит
                      /*if (checkPar(d.getFkHfp(), cd, "SI")) {
                          return d.getN1();
                      }*/
                      //if (d.getPar().getCd().equals(cd)) {
                      if (d.getPar().equals(par)) {
                          if (d.getPar().getTp().equals("NM")) {
                              if (d.getPar().getDataTp().equals("SI")) {
                                  return d.getN1();
                              } else {
                                      throw new WrongGetMethod("Попытка получить параметр "+cd+" не являющийся типом данного SI завершилась ошибкой");
                              }
                          } else {
                              throw new WrongGetMethod("Попытка получить параметр "+cd+" не являющийся типом NM завершилась ошибкой");
                          }
                      }
                  }
              }
              //если не найдено, то проверить, существует ли вообще этот параметр, в базе данных
              if (!isExByCd(cd)) {
                  throw new WrongGetMethod("Параметр "+cd+" не существует в базе данных");
              };
          } catch (WrongGetMethod e) {
              //TODO Auto-generated catch block
              e.printStackTrace();
          }
          return null;
      }
  
      @Cacheable(cacheNames="rrr1", key="{ #st.getKlsk(), #cd }")
      public synchronized Double getDbl(Storable st, String cd) {
          Par par = getByCD(cd);
          try {
              for (Dw d: st.getDw()) {
                  if (d.getPar().equals(par)) {
                      if (d.getPar().getTp().equals("NM")) {
                          if (d.getPar().getDataTp().equals("SI")) {
                              return d.getN1();
                          } else {
                                  throw new WrongGetMethod("Попытка получить параметр "+cd+" не являющийся типом данного SI завершилась ошибкой");
                          }
                      } else {
                          throw new WrongGetMethod("Попытка получить параметр "+cd+" не являющийся типом NM завершилась ошибкой");
                      }
                  }
              }
              //если не найдено, то проверить, существует ли вообще этот параметр, в базе данных
              if (!isExByCd(cd)) {
                  throw new WrongGetMethod("Параметр "+cd+" не существует в базе данных");
              };
          } catch (WrongGetMethod e) {
              //TODO Auto-generated catch block
              e.printStackTrace();
          }
          return null;
      }
  
  }
  </code></pre>
  
  <p>my pom.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;com.example&lt;/groupId&gt;
      &lt;artifactId&gt;demo2&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
  
      &lt;name&gt;demo&lt;/name&gt;
      &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.4.0.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
  
          &lt;!-- Spring --&gt;
          &lt;spring-framework.version&gt;4.2.5.RELEASE&lt;/spring-framework.version&gt;
  
          &lt;!-- Hibernate / JPA --&gt;
          &lt;hibernate.version&gt;5.1.0.Final&lt;/hibernate.version&gt;
  
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.samples&lt;/groupId&gt;
            &lt;artifactId&gt;Billing&lt;/artifactId&gt;
            &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
              &lt;exclusions&gt;
            &lt;exclusion&gt; 
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
            &lt;/exclusion&gt;
            &lt;exclusion&gt; 
              &lt;groupId&gt;log4j&lt;/groupId&gt;
              &lt;artifactId&gt;log4j&lt;/artifactId&gt;
            &lt;/exclusion&gt;
          &lt;/exclusions&gt; 
          &lt;/dependency&gt;
  
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web-services&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Hibernate --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-jpa-2.1-api&lt;/artifactId&gt;
              &lt;version&gt;1.0.0.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- https://mvnrepository.com/artifact/com.oracle/ojdbc14 --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.oracle&lt;/groupId&gt;
              &lt;artifactId&gt;oracle&lt;/artifactId&gt;
              &lt;version&gt;10.2.0.2.0&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;${hibernate.version}&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;5.2.0.Final&lt;/version&gt;
          &lt;/dependency&gt; 
  
  
          &lt;!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;net.sf.ehcache&lt;/groupId&gt;
              &lt;artifactId&gt;ehcache&lt;/artifactId&gt;
              &lt;version&gt;2.10.1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.javassist&lt;/groupId&gt;
              &lt;artifactId&gt;javassist&lt;/artifactId&gt;
              &lt;version&gt;3.18.1-GA&lt;/version&gt; 
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
                  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
                  &lt;version&gt;4.0.1.RELEASE&lt;/version&gt;
              &lt;/dependency&gt;
  
  
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  
  &lt;/project&gt;
  </code></pre>