gpt4 book ai didi

spring - Spring Data MongoDb-如何使用仅返回有效文档的“findAll”方法

转载 作者:行者123 更新时间:2023-12-03 15:59:48 25 4
gpt4 key购买 nike

我使用Spring数据mongo作为ORM来访问我的MongoDb。
我需要阅读一个MongoDb集合,其中包含我不管理内容及其有效性的文档。
而且我遇到了一个问题:文档并非全部有效,当我使用'findAll'方法时,我只是捕获了一个异常。除了这种行为,我想查找所有有效文档并拒绝无效文档。

例如,我有一个带有原始布尔值的bean,并且在文档中,此字段设置为String类型。因此,当我使用'findAll'时,我现在会得到一个例外,但是我想获得所有有效文档的列表。
我的意思是我的集合中的某些文档由于类型错误而无效-无法以bean Java类型进行转换。我只是使用一个布尔示例,但它也可能与int,long,所有原始Java类型一起出现。
有没有办法用Spring数据mongo做到这一点?

非常感谢,

这是堆栈空间:

java.lang.IllegalStateException:无法执行CommandLineRunner
    在org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:779)上[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    在org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760)上[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    在org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747)上[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    在org.springframework.boot.SpringApplication.run(SpringApplication.java:315)上[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    在org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    在org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)上[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    在hello.Application.main(Application.java:38)[classes /:na]
引起原因:java.lang.NullPointerException:null
    在hello.Customer_Accessor_sbf1mo.setProperty(来源不明)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    在org.springframework.data.mapping.model.ConvertingPropertyAccessor.setProperty(ConvertingPropertyAccessor.java:58)〜[spring-data-commons-1.13.3.RELEASE.jar:na]
    在org.springframework.data.mongodb.core.convert.MappingMongoConverter $ 1.doWithPersistentProperty(MappingMongoConverter.java:290)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    在org.springframework.data.mongodb.core.convert.MappingMongoConverter $ 1.doWithPersistentProperty(MappingMongoConverter.java:278)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    在org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:330)〜[spring-data-commons-1.13.3.RELEASE.jar:na]
    在org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:278)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    在org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:238)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    在org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:198)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    在org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:194)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    在org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:85)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    在org.springframework.data.mongodb.core.MongoTemplate $ ReadDbObjectCallback.doWith(MongoTemplate.java:2313)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    在org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1966)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    在org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1784)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    在org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1767)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    在org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:641)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    在org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:359)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    在org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:197)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    在org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:51)〜[spring-data-mongodb-1.10.3.RELEASE.jar:na]
    在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)〜[na:1.8.0_111]
    在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_111]
    在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_111]
    在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_111]
    在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:504)〜[spring-data-commons-1.13.3.RELEASE.jar:na]
    在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:489)〜[spring-data-commons-1.13.3.RELEASE.jar:na]
    在org.springframework.data.repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)〜[spring-data-commons-1.13.3.RELEASE.jar:na]
    在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    在org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)〜[spring-data-commons-1.13.3.RELEASE.jar:na]
    在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    在org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)〜[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    在org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)〜[spring-data-commons-1.13.3.RELEASE.jar:na]
    在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)〜[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)〜[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    在com.sun.proxy。$ Proxy43.findAll(来源不明)〜[na:na]
    在hello.Application.run(Application.java:58)[classes /:na]
    在org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776)上[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    ...省略了6个通用框架

最佳答案

您可以使用WHERE子句创建仅返回有效数据的自定义查询。

为此,您必须以这种方式创建ObjectRepositoryCustom.java接口:

@Repository
public interface ObjectRepositoryCustom {

//Where "o.field" is the field you have to check to decide if it's valid or not
@Query("SELECT o FROM Object o WHERE o.field = somethingThatValidsIt")
public List<Object> customFindAll();

}


然后,您进入 ObjectRepository.java并以这种方式添加 extends ObjectRepositoryCustom

//import your custom repository here
@Repository
public interface ObjectRepository extends JpaRepository<Object,Long>, ObjectRepositoryCustom {

}


请注意,我使用“对象”类型是因为我不知道您的班级名称。

然后,您可以像这样调用查询方法。

private final ObjectRepository objectRepository;
List<Object> result = objectRepository.customFindAll();


希望对您有所帮助!

关于spring - Spring Data MongoDb-如何使用仅返回有效文档的“findAll”方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44846917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com