Java DatastoreRepository无法保存对象,但所有find方法都会引发空指针异常
我一直在使用Spring应用程序,使用GCP数据存储作为我们的存储解决方案。为了简化代码,我正在考虑使用Spring数据存储库(我一直在努力) 因此,我创建了一个新的项目进行实验,使用我们已经建立的现有数据存储 我可以让.save()方法正常工作,但任何find方法都不起作用 我尝试了findAll、findByID,甚至是存储库的count方法,试图绕过它 在调试中运行它,看起来DatastoreTempate类无法找到该类 Pojo: 空存储库:Java DatastoreRepository无法保存对象,但所有find方法都会引发空指针异常,java,spring-boot,google-cloud-datastore,Java,Spring Boot,Google Cloud Datastore,我一直在使用Spring应用程序,使用GCP数据存储作为我们的存储解决方案。为了简化代码,我正在考虑使用Spring数据存储库(我一直在努力) 因此,我创建了一个新的项目进行实验,使用我们已经建立的现有数据存储 我可以让.save()方法正常工作,但任何find方法都不起作用 我尝试了findAll、findByID,甚至是存储库的count方法,试图绕过它 在调试中运行它,看起来DatastoreTempate类无法找到该类 Pojo: 空存储库: public interface Requi
public interface RequirementRepository extends DatastoreRepository
{}
服务:
@Service
public class GetAll
{
@Autowired
//DatastoreTemplate datastoreTemplate;
RequirementRepository requirementRepository;
public void getAll()
{
Requirement newReq = new Requirement();
newReq.setDescription("Check" + new Date());
newReq.setTitle("Check" + new Date());
requirementRepository.save(newReq);
System.out.println(requirementRepository.count()); //fails
Optional<Requirement> gotReq = requirementRepository.findById(newReq.getId()); //fails
if(gotReq.isPresent())
System.out.println(gotReq.get().getId());
List<Requirement> allReqs = (List<Requirement>) requirementRepository.findAll(); //fails
for (Requirement req : allReqs)
{
System.out.println(req.getId() + " , " + req.getTitle() + " , " + req.getDescription());
}
您只需要指定DatastoreRepository的参数
public interface RequirementRepository extends DatastoreRepository<Requirement, Long>
{}
public interface RequirementRepository扩展数据存储库
{}
请将它标记为有效答案我不得不等待它让我知道,你太快了!
Exception in thread "main" java.lang.NullPointerException
at org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate.findAllKeys(DatastoreTemplate.java:580)
at org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate.count(DatastoreTemplate.java:184)
at org.springframework.cloud.gcp.data.datastore.repository.support.SimpleDatastoreRepository.count(SimpleDatastoreRepository.java:110)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608)
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)
at com.sun.proxy.$Proxy44.count(Unknown Source)
at com.example.demo.GetAll.getAll(GetAll.java:32)
at com.example.demo.DemoApplication.main(DemoApplication.java:29)
public interface RequirementRepository extends DatastoreRepository<Requirement, Long>
{}