DatastoreRepository能够保存对象,但是所有的find方法都会抛出空指针异常
DatastoreRepository able to save objects, but all find methods throw a null pointer exception
我一直在使用 Spring 应用程序,使用 GCP 数据存储作为我们的存储解决方案。为了简化代码,我正在研究使用 Spring DataStore 存储库(我一直在努力做到这一点)
所以我创建了一个新项目来进行实验,使用我们设置的现有数据存储。
我可以让 .save() 方法正常工作,但任何查找方法都不起作用。
我已经尝试了 findAll、findByID 甚至存储库的 count 方法来尝试绕过它。
运行 正在debug,貌似DatastoreTempate class 找不到那种。
波乔:
@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(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>
{}
我一直在使用 Spring 应用程序,使用 GCP 数据存储作为我们的存储解决方案。为了简化代码,我正在研究使用 Spring DataStore 存储库(我一直在努力做到这一点)
所以我创建了一个新项目来进行实验,使用我们设置的现有数据存储。
我可以让 .save() 方法正常工作,但任何查找方法都不起作用。
我已经尝试了 findAll、findByID 甚至存储库的 count 方法来尝试绕过它。
运行 正在debug,貌似DatastoreTempate class 找不到那种。
波乔:
@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(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>
{}