JPA Query By Example (QBE) 不支持日期查询

Posted 老邋遢

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JPA Query By Example (QBE) 不支持日期查询相关的知识,希望对你有一定的参考价值。

JPA Query By Example (QBE) 不支持日期查询

文章目录

一. 先说结论

Query By Example(下文统称QBE)

只支持字符串类型的Example查询, 如果要加另外的例如日期类型的查询, 需要通过构建额外的Specification来实现

1.1 在原有的repo上继承JpaSpecificationExecutor

public interface TranxlogRepository extends JpaRepository<Tranxlog, Long>, JpaSpecificationExecutor<Tranxlog> 

1.2 构建Specification

public Specification<TranxLog> getSpecFromDatesAndExample(
  LocalDateTime from, LocalDateTime to, Example<TranxLog> example) 
    return (root, query, builder) -> 
         final List<Predicate> predicates = new ArrayList<>();
         if (from != null) 
            predicates.add(builder.greaterThan(root.get("dateField"), from));
         
         if (to != null) 
            predicates.add(builder.lessThan(root.get("dateField"), to));
         
         predicates.add(QueryByExamplePredicateBuilder.getPredicate(root, builder, example));
         return builder.and(predicates.toArray(new Predicate[predicates.size()]));
    
;

1.3 调用repo.findAll方法时传入Specification

public Page<TranxLog> findAllByConditions(TranxReportFormModel formModel, Pageable page) 
    ExampleMatcher matcher = ExampleMatcher.matching()
            .withNullHandler(ExampleMatcher.NullHandler.IGNORE)
            .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
            .withIgnoreCase()
            .withIgnoreNullValues();
    Example<TranxLog> example = Example.of(formModel.getTranxLog(), matcher);
    return tranxlogRepository.findAll(getSpecFromDatesAndExample(from, to, Example.of(formModel.getTranxLog(), matcher)), page);

二. 问题描述

QBE是JPA给我们提供的一个通过样例查询的方式, 我们可以通过构建一个 entity “案例”, 然后JPA会通过这个entity存在的字段为我们构建查询.

下图为JpaRepository提供的Example查询方法定义

但是通过查看ExampleMatcher源码发现只有StringMatcher一种实现, 这意味着我们只能对一些字符类型的列进行模糊匹配或者全匹配(也支持REGEX形式的匹配).

如果要对日期类型进行比较则不支持, 需要我们手动构建一个包含Example查询的Specification(详情参照上文, 不赘述)

参考资料

以上是关于JPA Query By Example (QBE) 不支持日期查询的主要内容,如果未能解决你的问题,请参考以下文章