简体   繁体   English

如何在嵌套字段未完全填充的情况下使用示例匹配器

[英]How to use Example Matcher with nested fields not fully filled

I've got the following structure:我有以下结构:

@Document
class A {
String value1;
B b;
}

class B {
String value1;
String value2;
Integer value3;
}

Then I save a document with the following values:然后我保存一个包含以下值的文档:

{
value1: "val",
b: {
value1: "val1",
value2: "val2"
value3: 156
}
}

When I create an example with both values in B filled I get a match, when I fill only A.value1 and B.value1 I don't get a match.当我创建一个包含 B 中两个值的示例时,我得到一个匹配项,当我只填充 A.value1 和 B.value1 时,我没有得到匹配项。 How can I make the match ingore null values in nested fields?如何在嵌套字段中匹配 null 值?

Search:搜索:

repository.findAll(Example.of(new A("val", new B("val1", null, null)))

Doesn't match because some of the B fields are not filled.不匹配,因为某些 B 字段未填写。

Search:搜索:

repository.findAll(Example.of(new A("val", new B("val1", "val2", 156)))

Matches as all the fields are filled.匹配所有字段。

This should work by definining ExampleMatcher of your own:这应该通过定义您自己的 ExampleMatcher 来工作:

ExampleMatcher exampleMatcher = ExampleMatcher.matchingAll();

Then query would be like:然后查询就像:

repository.findAll(
    Example.of(new A("val", new B("val1", null, null)), exampleMatcher));

See API docs :请参阅API 文档

Create a new ExampleMatcher including all non-null properties by default matching all predicates derived from the example.创建一个新的 ExampleMatcher ,包括所有非空属性,默认情况下匹配从示例派生的所有谓词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM