Code: Select all
public Page searchFoo(@NotNull Foo probe, @NotNull Pageable pageable) {
Specification spec = Specification.where(null); // is this ok?
if (probe.getName() != null) {
spec.and(FooSpecs.containsName(probe.getName()));
}
if (probe.getState() != null) {
spec.and(FooSpecs.hasState(probe.getState()));
}
//and so on...
return fooRepo.findAll(spec, pageable);
}
< /code>
Es besteht die Möglichkeit, dass keine Filter angegeben sind, daher würde ich alles ohne Filterung auflisten. Wenn ich das im Sinn habe, wie sollte ich Spec
foospecs:
Code: Select all
public class PrescriptionSpecs {
public static Specification containsCode(String code) {
return (root, criteriaQuery, criteriaBuilder) ->
criteriaBuilder.like(root.get(Prescription_.code), "%" + code + "%");
}
// some methods matching objects...
public static Specification hasContractor(Contractor contractor) {
return (root, criteriaQuery, criteriaBuilder) ->
criteriaBuilder.equal(root.get(Prescription_.contractor), contractor);
}
//... also some methods that access nested objects, not sure about this
public static Specification containsUserCode(String userCode) {
return (root, criteriaQuery, criteriaBuilder) ->
criteriaBuilder.like(root.get(Prescription_.user).get(User_.code), "%" + userCode + "%");
}
}