#Spring Data JPA: How to get default expression from CriteriaBuilder inside Specification

1 messages · Page 1 of 1 (latest)

gray pike
#

I building a search query for area, whether it's just greater than start bound, lesser than end bound or between them. I am refactoring the method and wrapping if statements inside the single Specification predicate body. Before that an Specification was returned in each if statement.

private Specification<Listing> whereArea(Specification<Listing> spec, ListingSearchDTO dto) {
    return (root, _, builder) -> {
        Path<Integer> area = root.get("apartment").get("area");

        if (dto.getM2From() != null && dto.getM2To() != null)
            return builder.between(area, dto.getM2From(), dto.getM2To());
        if (dto.getM2From() != null)
            return builder.ge(area, dto.getM2From());
        if (dto.getM2To() != null)
            return builder.le(area, dto.getM2To());
        return builder.; // return something here
    };
}

What should I return as the default result? I can't seem to find anything about that in the docs.

ripe cairnBOT
#

<@&1004656351647117403> please have a look, thanks.

gray pike
#

The code looked like this before:

private Specification<Listing> whereArea(Specification<Listing> spec, ListingSearchDTO dto) {
    if (dto.getM2From() != null && dto.getM2To() != null)
        return (root, _, builder) -> builder.between(root.get("apartment").get("area"), dto.getM2From(), dto.getM2To());
    if (dto.getM2From() != null)
        return (root, _, builder) -> builder.ge(root.get("apartment").get("area"), dto.getM2From());
    if (dto.getM2To() != null)
        return (root, _, builder) -> builder.le(root.get("apartment").get("area"), dto.getM2To());
    return spec;
}
gray pike
#

Will this be alright?

return spec.toPredicate(root, query, builder);
gray pike
#

Spring Boot: How to get default expression from CriteriaBuilder inside Specification

#

Spring Data JPA: How to get default expression from CriteriaBuilder inside Specification

gray pike
#

@everyone

left spade
#

Give up pinging everyone. Helpers automatically see the posts of tags they've registered interest in.

#

Can you imagine how annoying it would be if the entire server got pinged all of the time (for help tags that a portion of the server may not be interested in).