2017-10-09 8 views
0

저는 ehcache를 사용하고있는 스프링 부트 응용 프로그램을 가지고 있습니다. 나는 단지 하나 개의 엔티티 클래스가 있다면으로 Ehcache는 잘 작동하지만 난 1 개 이상의 엔티티 클래스가있는 경우으로 Ehcache가 작동하지 않습니다와 나는 아래의 오류가 무엇입니까 : -Ehcache가 스프링 부트 응용 프로그램과 함께 작동하지 않습니다.

java.lang.ClassCastException: com.myapp.beans.Contact cannot be cast to com.myapp.beans.Department 
    at com.sun.proxy.$Proxy102.findOneById(Unknown Source) ~[na:na] 
    at com.myapp.service.impl.DepartmentServiceImpl.show(DepartmentServiceImpl.java:19) ~[classes/:na] 

내 자바 코드 : -

DepartmentRepository.java

@Repository 
public interface DepartmentRepository extends JpaRepository<Department, Integer> { 

    @Cacheable(value="appCache") 
    @Query("select d from Department d where d.id=:id") 
    Department findOneById(@Param("id") int id); 
} 

ContactRepository

-

ehcache.xml

<ehcache> 
    <cache name="appCache" maxBytesLocalHeap="50m" timeToLiveSeconds="100"></cache> 
</ehcache> 

전체 코드에서 사용할 수 있습니다. 어떤 도움을 주셔서 감사합니다.

답변

1

캐시의 이름이 동일하기 때문에 "키 충돌"이 발생했을 가능성이 큽니다. 나는이 같은 캐시의 이름을 변경하는 것이 좋습니다 :

@Cacheable(value="appCache1") 
    @Query("select d from Department d where d.id=:id") 
    Department findOneById(@Param("id") int id); 

@Cacheable(value = "appCache2") 
    @Query("select c from Contact c where c.id=:id") 
    Contact findOneById(@Param("id") int id); 

은 또한 당신이 ehcache.xml

또 다른 방법으로 이러한 캐시의 각을 만들어야합니다 - 캐시에 사용되는 키, 당신은 그것을 here을에 대해 읽을 수 있습니다