I am doing a simple caching in my springboot application. Here I am trying to perform findall,delete and update operations with JPA. At the same time I am trying to update my cache also after performing each operations.
This is my entity
@Entity
@Table(name = "cars")
public class Cars implements Serializable{
@Id
@Column(name = "carId")
private long carId;
@Column(name = "name")
private String name;
..........getters and setters.......
This is my service
@Service
public class CarServiceImpl implements CarService{
@Autowired
private CarRepository carRepo;
@Override
@Transactional
@Cacheable(value = "cars")
public Cars save(Cars car) {
// TODO Auto-generated method stub
System.out.println("reciving from DB for save");
return carRepo.save(car);
}
@Override
@Transactional
@CacheEvict(value="cars" ,key="#id")
public void delete(long id) {
System.out.println("reciving from DB for delete");
// TODO Auto-generated method stub
carRepo.deleteById(id);
}
@Override
@Transactional
@CachePut(value="cars",key="#id")
public Cars update(Cars car,long id) {
System.out.println("reciving from DB for update");
// TODO Auto-generated method stub
return carRepo.save(car);
}
@Override
@Transactional
@Cacheable(value="cars",key="#id")
public List<Cars> getAll(long id) {
System.out.println("reciving from DB for show");
// TODO Auto-generated method stub
return carRepo.findAll();
}
this is the error I am getting when I perform update operation to add a new entity and then call getAll method where it loads from cache .
java.lang.ClassCastException: com.example.springCaching.entity.Cars cannot be cast to java.util.List
at com.example.springCaching.serviceImpl.CarServiceImpl$$EnhancerBySpringCGLIB$$80d45199.getAll(<generated>) ~[classes/:na]
at com.example.springCaching.contoller.CarController.show(CarController.java:39) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_161]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_161]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_161]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:798) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.1.14.RELEASE.jar:5.1.14.RELEASE]
at org.springframework.web.se