Spring JPA ExampleMatcher compare date condition
Asked Answered
D

2

12

I'm using Spring JPA and get list of data by using Example Matcher. Source code below:

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(example, page);
    }

Now, I have search page, which have formDate and toDate and it has to be compared with field DateTime in TranxLog. I tried to use .withMatcher() but cannot find a way to compare a date.

Any idea? Thanks.

Delfeena answered 31/8, 2017 at 4:6 Comment(0)
B
15

You can extends from JpaSpecificationExecutor too and get the Predicate with QueryByExamplePredicateBuilder from Example.

In your repository:

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

In your serviceImp

public Specification<TranxLog> getSpecFromDatesAndExample(
  LocalDateTime from, LocalDateTime to, Example<TranxLog> example) {

    return (Specification<TranxLog>) (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()]));
    }
};

In your serviceImp too:

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);
}
Bedstraw answered 13/5, 2020 at 14:19 Comment(1)
Worked for me as expected. I wanted to filter from the records with a from date (in epoch time) provided in the API endpoint. Thanks!Javed
J
4

You can't do that with query-by-example. Probably the best is to construct a Specification

Jordanjordana answered 18/1, 2018 at 14:4 Comment(2)
it got deprecated. And how to combine both Example and Speicification? In date I want range, but in the primary key, I want to give full value.Melone
You can't combine query by example and Specification but nothing is keeping you to create a specification from an entity. Also Specification isn't deprecated, only Specifications is (note the s at the end).Jordanjordana

© 2022 - 2024 — McMap. All rights reserved.