数据存储存储库能够保存对象,但所有查找方法都会引发空指针异常



我一直在使用Spring App,使用GCP数据存储作为我们的存储解决方案。为了简化代码,我正在考虑使用Spring DataStore Repository(我一直在努力工作(

因此,我创建了一个新项目来试验,使用我们设置的现有数据存储。

我可以让 .save(( 方法正常工作,但任何查找方法都不起作用。

我已经尝试了findAll,findByID,甚至是存储库的count方法来尝试绕过它。

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

波乔:

@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>
{}

最新更新