首页  编辑  

JPA repository.findAll example 无法返回数据

Tags:   Date Created:
Springboot中, JPA中,用 repository.findAll(Example.of(dto)) 无法返回数据的问题:

默认情况下,Example 采用的是精确匹配,对于dto中 null 的字段也是精确匹配,可能导致无数据返回,我们可以设置忽略dto中null的字段
ExampleMatcher matcher = ExampleMatcher.matching().withIgnoreNullValues();
repository.findAll(Example.of(dto, matcher));

如果上述方法还是无法返回数据,则用UntypedExampleMatcher替代ExampleMatcher,例如:
ExampleMatcher matcher = UntypedExampleMatcher.matching().withIgnoreNullValues();
repository.findAll(Example.of(dto, matcher));

尤其是在Mongodb的场景当中,需要用 UntypedExampleMatcher 替代 ExampleMatcher。