【发布时间】:2018-11-03 12:23:43
【问题描述】:
我正在尝试使用 Spring-JPA,如下面的代码所示。
@Repository
public interface EmployeeCrud extends CrudRepository<Employee, Integer> {
@Cacheable(cacheNames = "emp_by_last_name, key = "#lastName")
List<Employee> findAllByLastName(@Param("lastName") String lastName);
}
由于接口没有参数名称(除非编译时启用调试信息),由于以下异常,我无法获取数据。
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)
即使我使用@Param 或@P 注释,CacheOperationExpressionEvaluator 也无法解决它,因为它使用内部使用StandardReflectionParameterNameDiscoverer 和LocalVariableTableParameterNameDiscoverer 的DefaultParameterNameDiscoverer。
如果是这样,它也会使用AnnotationParameterNameDiscoverer,@Param 会被解析。
在不启用编译器调试信息或实现EmployeeCrud 接口的情况下,我们还需要哪些其他解决方案才能完成这项工作?
【问题讨论】:
-
如果删除 key = "#lastName" 会发生什么?
-
它会起作用的。在某些情况下,表达式可能如下所示。在这种情况下,它可能不起作用。
#entity.lastName -
你的意思是什么场景?
-
我在之前的评论中已经提到过。如果表达式是
#entity.lastName,那么在这种情况下它可能不起作用。 -
但只要你有专用的缓存,这就是 irelevatn
标签: spring interface crud spring-cache