javajpaspring-data-jpaspecificationscriteriaquery

Java Specification CriteriaBuilder complex query


I'm not very experienced with Specification, builder, query, I have to do a rather complex query like this:

select * from table where
   code in ('code1',  'code2' //codes) and
    (
        (
            date between "2020-03-23 //from" and "2020-03-30 //to"
            and
            status in ('Status1' , 'Status2' //status)
        )
        or
        (
            date between "2021-03-23" and "2021-03-30"
            and
            status in ('Status3' , 'Status4')
        )
    )

And i have a DTO like this:

public class SearchCriteria {

@Embedded
private Filters filters;

@Embeddable
@Getter
@Setter
public static class Filters {
    private List<String> codes;
    private List<TimePeriod> timePeriods;
}

@Embeddable
@Getter
@Setter
public static class TimePeriod {
    private List<String> status;
    private StartDate startDate;
}

@Embeddable
@Getter
@Setter
public static class StartDate {
    private LocalDate from;
    private LocalDate to;
}

It's very hard for me. I'm trying everything. I preferred to show you a specific case so as not to run into misunderstanding. Could someone help me? I would appreciate very much!

I don't need to use the Specification, I just need to be able to reproduce that query example, the Specification just seemed like the best choice.

Thank u all.


Solution

  • I think you're on the right track, the criteria class looks fine. Here's how you could use it in a method to build the JPA criteria and execute a query using the repository corresponding to your entity:

    public void query(List<String> codes, List<TimePeriod> timePeriods) {
        // build the code filter
        Specification<Table> codeSpec = (root, query, criteriaBuilder) -> {
            Path<String> codeField = root.get("code");
            var codePredicate = criteriaBuilder.in(codeField);
            codes.forEach(code -> codePredicate.value(code));
            return codePredicate;
        };
        // iterate over the time periods
        var timePeriodSpec = timePeriods.stream().map(timePeriod -> {
            Specification<Table> dateSpec = (root, query, criteriaBuilder) -> {
                Path<LocalDate> dateField = root.get("date");
                return criteriaBuilder.between(dateField, timePeriod.startDate.from, timePeriod.startDate.to);
            };
            Specification<Table> statusSpec = (root, query, criteriaBuilder) -> {
                Path<String> statusField = root.get("status");
                var statusPredicate = criteriaBuilder.in(statusField);
                timePeriod.status.forEach(status -> statusPredicate.value(status));
                return statusPredicate;
            };
            // combine the date and status filter
            return dateSpec.and(statusSpec);
        })
        .reduce(Specification::or).get(); // chain the time period filters together
        
        var fullSpec = codeSpec.and(timePeriodSpec);
    
        var result = tableRepository.findAll(fullSpec, Pageable.unpaged());
    }
    

    You also need to make sure your repository implements the JpaSpecificationExecutor interface, but you've probably figured that out already.