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

How to use Example Matcher with nested fields not fully filled

我有以下结构:

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

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

然后我用以下值保存文档:

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

当我创建一个包含 B 中两个值的示例时,我得到一个匹配项,当我只填充 A.value1 和 B.value1 时,我没有得到一个匹配项。如何使嵌套字段中的匹配项不包含空值?

搜索:

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

不匹配,因为某些 B 字段未填写。

搜索:

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

匹配所有字段。

这应该通过定义您自己的 ExampleMatcher 来工作:

ExampleMatcher exampleMatcher = ExampleMatcher.matchingAll();

那么查询将是这样的:

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

参见API docs

Create a new ExampleMatcher including all non-null properties by default matching all predicates derived from the example.