I'd like to add a @CacheEvict()
decorator to my save / saveAll methods from my repository.
I've tried to override the methods by using custom implementation by reading this thread but I don't want to rewrite the method implementation, just call the default one with more behavior. None of the solutions provided work well for my case, or require far too much customization.
@Transactional(readOnly = true)
@Repository
public interface MyRepository extends JpaRepository<MyEntity, Long> {
// This is working fine as its a custom query method
@Cacheable(value = "ENTITIES")
MyEntity findByCategory(String category);
// This isn't working as it won't implement the function correctly. Save don't work anymore here.
@CacheEvict(value = "ENTITIES", key = "#entity.hashCode()")
<S extends MyEntity> S save(S entity);
}
I could call the cache eviction manually from the method's body, but I can't just override the save()
method and call the super.save()
since I only use interfaces and let Spring generate the implementation.
Any advices?
in the save method. it should be working fine– Na Felix Wimpy Wijaya Jun 25 '19 at 08:54