I am trying to use Spring-JPA as shown in the below code.
@Repository
public interface EmployeeCrud extends CrudRepository<Employee, Integer> {
@Cacheable(cacheNames = "emp_by_last_name, key = "#lastName")
List<Employee> findAllByLastName(@Param("lastName") String lastName);
}
Since interfaces do not have parameter names (unless compiled with debug info enabled), I am not able fetch the data because of the below exception.
java.lang.IllegalArgumentException: Null key returned for cache operation (maybe you are using named params on classes without debug info?) Builder[public abstract java.util.List EmployeeCrud.findAllByLastName(java.lang.String)] caches=[emp_by_last_name] | key='#lastName' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
at org.springframework.cache.interceptor.CacheAspectSupport.generateKey(CacheAspectSupport.java:561)
at org.springframework.cache.interceptor.CacheAspectSupport.findCachedItem(CacheAspectSupport.java:502)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:389)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:327)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
Even if I use @Param
or @P
annotation, CacheOperationExpressionEvaluator
is not able to resolve it since it uses DefaultParameterNameDiscoverer
which internally uses StandardReflectionParameterNameDiscoverer
and LocalVariableTableParameterNameDiscoverer
.
Had it been it would have also used AnnotationParameterNameDiscoverer
, @Param
would have been parsed.
What other solutions do we have to make this work without enabling compiler debug info or implementing EmployeeCrud
interface?