spring-data-hazelcast @Query 注释给出 NullPointerException
spring-data-hazelcast @Query annotation gives NullPointerException
https://github.com/hazelcast/spring-data-hazelcast SpringBootTest 中使用的库在如下使用 findAll() 时成功检索列表。
@Override
public List<Store> getStores() {
return storeRepository.findAll();
}
然而,当 @Query
注释如下使用时,它会给出 NullPointerException。
@Override
public List<Store> (String cityId, String countryId) {
return storeRepository.getStores(cityId,countryId);
}
存储库
public interface StoreRepository extends HazelcastRepository<Store,String> {
@Query("cityId=%s and countryId=%s")
public List<Store> getStores(String cityId, String countryId);
}
例外如下。
java.lang.NullPointerException: Retrieving a map instance with a null name is not allowed!
at com.hazelcast.util.Preconditions.checkNotNull(Preconditions.java:59)
at com.hazelcast.instance.HazelcastInstanceImpl.getMap(HazelcastInstanceImpl.java:183)
at com.hazelcast.instance.HazelcastInstanceProxy.getMap(HazelcastInstanceProxy.java:99)
at org.springframework.data.hazelcast.repository.support.StringBasedHazelcastRepositoryQuery.getMap(StringBasedHazelcastRepositoryQuery.java:68)
at org.springframework.data.hazelcast.repository.support.StringBasedHazelcastRepositoryQuery.execute(StringBasedHazelcastRepositoryQuery.java:48)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:605)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke(RepositoryFactorySupport.java:595)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
是否缺少 属性?如果是这样,为什么它与 findAll() 一起使用?
Default findAll()
method calls Spring Data in SimpleKeyValueRepository
instance and it consumes KeyValueTemplate(this instance has HazelcastKeyValueAdapter)
findAll() method.
So, @Query
annoted queries Hazelcast Spring data implementation's HazelcastQueryMethod.class looks for @KeySpace
annotation with value. StringBasedHazelcastRepositoryQuery.class getMap
method calls hazelcastInstance's getMap
method with that keySpace's value which is map key.
简而言之,您需要为您的商店添加 @KeySpace("yourMapName")
注释 class。
额外信息;当你不把 @KeySpace("yourMapName")
放在你的 class 上时,它会存储确切的包名称,如 com.yourpackage.model.Store.
https://github.com/hazelcast/spring-data-hazelcast SpringBootTest 中使用的库在如下使用 findAll() 时成功检索列表。
@Override
public List<Store> getStores() {
return storeRepository.findAll();
}
然而,当 @Query
注释如下使用时,它会给出 NullPointerException。
@Override
public List<Store> (String cityId, String countryId) {
return storeRepository.getStores(cityId,countryId);
}
存储库
public interface StoreRepository extends HazelcastRepository<Store,String> {
@Query("cityId=%s and countryId=%s")
public List<Store> getStores(String cityId, String countryId);
}
例外如下。
java.lang.NullPointerException: Retrieving a map instance with a null name is not allowed!
at com.hazelcast.util.Preconditions.checkNotNull(Preconditions.java:59)
at com.hazelcast.instance.HazelcastInstanceImpl.getMap(HazelcastInstanceImpl.java:183)
at com.hazelcast.instance.HazelcastInstanceProxy.getMap(HazelcastInstanceProxy.java:99)
at org.springframework.data.hazelcast.repository.support.StringBasedHazelcastRepositoryQuery.getMap(StringBasedHazelcastRepositoryQuery.java:68)
at org.springframework.data.hazelcast.repository.support.StringBasedHazelcastRepositoryQuery.execute(StringBasedHazelcastRepositoryQuery.java:48)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:605)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke(RepositoryFactorySupport.java:595)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
是否缺少 属性?如果是这样,为什么它与 findAll() 一起使用?
Default
findAll()
method calls Spring Data inSimpleKeyValueRepository
instance and it consumesKeyValueTemplate(this instance has HazelcastKeyValueAdapter)
findAll() method.So,
@Query
annoted queries Hazelcast Spring data implementation's HazelcastQueryMethod.class looks for@KeySpace
annotation with value. StringBasedHazelcastRepositoryQuery.classgetMap
method calls hazelcastInstance'sgetMap
method with that keySpace's value which is map key.
简而言之,您需要为您的商店添加 @KeySpace("yourMapName")
注释 class。
额外信息;当你不把 @KeySpace("yourMapName")
放在你的 class 上时,它会存储确切的包名称,如 com.yourpackage.model.Store.