0

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
TJ32
  • 293
  • 2
  • 7
  • 20
  • 1
    In update your puting an instance of Cars in your cache, in getAll your caching a List of cars. That won't work. I also don't get why your getAll method has an id parameter? – TomStroemer Apr 30 '20 at 15:05
  • is there a way through which i can achieve it ?..the delete method is working fine and changes are reflected in cache too. – TJ32 Apr 30 '20 at 17:14
  • 1
    Your `getAll` method is annotated with `@Cacheable(value="cars",key="#id")` which has `#id` as the key. Also, as @TomStroemer has pointed out, your `getAll` method takes `id` as a parameter which seems to be used to get a single `Cars` from cache which you're trying to typecast to `List`. Take a look at this: https://stackoverflow.com/questions/51237672/spring-cache-all-elements-in-list-separately – kayvis Apr 30 '20 at 19:06
  • thanks kayvis ..... i guess refreshing the cache is the only solution – TJ32 May 01 '20 at 06:35

0 Answers0