Using Spring data's Specification
interface I was able to approximate the use of query by example. Here's a PersonSpec
class which implements Specification
and requires an "example" person in order to setup the Predicate
returned by the Specification
:
public class PersonSpec implements Specification<Person> {
private final Person example;
public PersonSpec(Person example) {
this.example = example;
}
@Override
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<>();
if (StringUtils.isNotBlank(example.getLastName())) {
predicates.add(cb.like(cb.lower(root.get(Person_.lastName)), example.getLastName().toLowerCase() + "%"));
}
if (StringUtils.isNotBlank(example.getFirstName())) {
predicates.add(cb.like(cb.lower(root.get(Person_.firstName)), example.getFirstName().toLowerCase() + "%"));
}
if (example.getEmployed() != null) {
predicates.add(cb.equal(root.get(Person_.employed), example.getEmployed()));
}
if (example.getDob() != null) {
predicates.add(cb.equal(root.get(Person_.dob), example.getDob()));
}
return andTogether(predicates, cb);
}
private Predicate andTogether(List<Predicate> predicates, CriteriaBuilder cb) {
return cb.and(predicates.toArray(new Predicate[0]));
}
}
The repository is simply:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface PersonRepository extends JpaRepository<Person, Long>, JpaSpecificationExecutor {}
Usage example:
Person example = new Person();
example.setLastName("James");
example.setEmployed(true);
PersonSpec personSpec = new PersonSpec(example);
List<Person> persons = personRepository.findAll(personSpec);