gpt4 book ai didi

java - spring-data-hazelcast @Query 注释给出 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 19:04:45 24 4
gpt4 key购买 nike

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$3(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)

是否缺少属性(property)?如果是这样,为什么它可以与 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.

简而言之,您需要向 Store 类添加 @KeySpace("yourMapName") 注解。

<小时/>

额外信息;当您不将 @KeySpace("yourMapName") 放在您的类上时,它将使用确切的包名称进行存储,例如 com.yourpackage.model.Store。

关于java - spring-data-hazelcast @Query 注释给出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59580063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com