问题
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.
I can get the .save() method working fine, but any find methods do not work.
I have tried findAll, findByID and even the count method of the repository to try and get around it.
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;
Empty Repository:
public interface RequirementRepository extends DatastoreRepository
{}
Service:
@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());
}
}
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)
回答1:
You just need to specify the arguments for DatastoreRepository
public interface RequirementRepository extends DatastoreRepository<Requirement, Long>
{}
来源:https://stackoverflow.com/questions/57993447/datastorerepository-able-to-save-objects-but-all-find-methods-throw-a-null-poin