DatastoreRepository能够保存对象,但是所有查找方法都将引发空指针异常 [英] DatastoreRepository able to save objects, but all find methods throw a null pointer exception

查看:55
本文介绍了DatastoreRepository能够保存对象,但是所有查找方法都将引发空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Spring App,使用GCP数据存储作为我们的存储解决方案.为了简化代码,我正在研究使用Spring DataStore存储库(我一直在辛苦地做到这一点)

I have been working with a Spring App, using GCP Datastore as our storage solution. In order to streamline the code, I am looking into using the Spring DataStore Repository (I have been doing it all the hard way)

因此,我使用我们已经建立的现有数据存储区,创建了一个新项目进行实验.

So I have created a new project to experiment, using the existing datastore we have set up.

我可以使.save()方法正常工作,但是任何find方法都不起作用.

I can get the .save() method working fine, but any find methods do not work.

我尝试过findAll,findByID甚至是存储库的count方法来尝试解决它.

I have tried findAll, findByID and even the count method of the repository to try and get around it.

在调试中运行它,看来DatastoreTempate类无法找到该类型.

Running it in debug, it looks like the DatastoreTempate class is unable to find the kind.

Pojo:

@Entity(name = "Requirement")
public class Requirement
{

//variables
@Id
@Field(name = "Requirement_id")
private Long id;

private String title;

private String description;

空存储库:

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());
    }

}

运行count方法时,我们会收到以下异常:

When running the count method we get the following exception:

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)

推荐答案

您只需指定DatastoreRepository的参数

You just need to specify the arguments for DatastoreRepository

public interface RequirementRepository extends DatastoreRepository<Requirement, Long>
{}

这篇关于DatastoreRepository能够保存对象,但是所有查找方法都将引发空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆