我一直在使用Spring的应用程序,使用GCP数据存储作为我们的存储解决方案。为了精简代码,我期待到使用Spring数据存储库(我已经做了所有的努力方式)

所以,我创建了一个新的项目进行试验,利用我们已经建立了现有的数据存储。

我可以得到.save()方法做工精细,但找不到任何方法不起作用。

我已经试过的findAll,findByID甚至仓库的计数方法,试图围绕它得到。

在调试运行它,它看起来像DatastoreTempate类是无法找到的那种。

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

}

运行计数方法时,我们得到以下异常:

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参数

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