spring-data-jpa - 如何通过示例配置查询以包含空值?

标签 spring-data-jpa query-by-example

我有以下匹配器:

Example.of(
    user,
    ExampleMatcher.matching()
                  .withMatcher("name", contains().ignoreCase())
                  .withMatcher("phoneNumber", contains())
)

除了 null 之外,它工作正常值。例如,它不返回电话号码为 NULL 的用户。 .

我试图跟随以包含 NULL值,但它不起作用:
Example.of(
    user,
    ExampleMatcher.matching()
                  .withMatcher("name", contains().ignoreCase())
                      .withIncludeNullValues()
                  .withMatcher("phoneNumber", contains())
                      .withIncludeNullValues()
)

生成的SQL如下:
select
    user0_.id as id1_9_,
    user0_.document_expiry_date as document2_9_,
    user0_.document_type as document3_9_,
    user0_.document_url as document4_9_,
    user0_.email as email5_9_,
    user0_.name as name6_9_,
    user0_.phone_number as phone_nu7_9_
from
    user user0_
where
    (lower(user0_.name) like ?)
    and (user0_.id is null)
    and (user0_.document_type is null)
    and (user0_.document_url is null)
    and (user0_.email is null)
    and (user0_.phone_number like ?)
    and (user0_.document_expiry_date is null)

如何配置它以包含带有 NULL 的行列也是?

最佳答案

遇到了同样的问题,但我认为没有办法实现这个目标。下面是 withMatcher 方法的源代码:

public ExampleMatcher withMatcher(String propertyPath, ExampleMatcher.GenericPropertyMatcher genericPropertyMatcher) {
    Assert.hasText(propertyPath, "PropertyPath must not be empty!");
    Assert.notNull(genericPropertyMatcher, "GenericPropertyMatcher must not be empty!");
    ExampleMatcher.PropertySpecifiers propertySpecifiers = new ExampleMatcher.PropertySpecifiers(this.propertySpecifiers);
    ExampleMatcher.PropertySpecifier propertySpecifier = new ExampleMatcher.PropertySpecifier(propertyPath);
    if (genericPropertyMatcher.ignoreCase != null) {
        propertySpecifier = propertySpecifier.withIgnoreCase(genericPropertyMatcher.ignoreCase);
    }

    if (genericPropertyMatcher.stringMatcher != null) {
        propertySpecifier = propertySpecifier.withStringMatcher(genericPropertyMatcher.stringMatcher);
    }

    if (genericPropertyMatcher.valueTransformer != null) {
        propertySpecifier = propertySpecifier.withValueTransformer(genericPropertyMatcher.valueTransformer);
    }

    propertySpecifiers.add(propertySpecifier);
    return new ExampleMatcher(this.nullHandler, this.defaultStringMatcher, propertySpecifiers, this.ignoredPaths, this.defaultIgnoreCase, this.mode);
}

此方法返回一个 ExampleMatcher,其中包含应用于所有字段的“全局”nullHandler。这意味着,您不能为某些特殊字段指定不同的 nullHander。

关于spring-data-jpa - 如何通过示例配置查询以包含空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47805050/

相关文章:

java - 如何在 Spring 中使用 JPA 存储库从服务器检索 blob

java - 如何在 native 查询 JPA 中传递参数

java - 有没有一种简单的方法可以在 iBATIS 中进行示例查询?

java - 替换 MarkLogic 中弃用的 KeyValueQueryDefinition 以使用 Query By Example

Java:字符串:没有给出好的结果

java - 如何让Spring bean扩展一个通用的抽象类?

找不到 Spring 数据 jpa 存储库属性

java - 无法将 IBMi 机器与 Spring Data 和 JPA 连接

java - QueryByExampleExecutor<T>接口(interface)方法在Spring data JPA中的使用案例

spring-boot - Spring Jpa Query by Example 集合