How to get default expression from CriteriaBuilder inside Specification

20 hours ago 3
ARTICLE AD BOX

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.

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; }
Read Entire Article