Spring Data JPA: Query by Example?

前端 未结 3 1919
渐次进展
渐次进展 2020-12-02 19:59

Using Spring Data JPA can I do a query by example where a particular entity instance is used as the search criteria?

For example (no pun intended), if I have a

相关标签:
3条回答
  • 2020-12-02 20:42

    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);
    
    0 讨论(0)
  • 2020-12-02 20:52

    This is now possible with Spring Data. Check out http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example

    Person person = new Person();                         
    person.setLastname("Smith");                          
    Example<Person> example = Example.of(person);
    List<Person> results = personRepository.findAll(example);
    

    Note that this requires very recent 2016 versions

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.10.1.RELEASE</version>       
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>1.12.1.RELEASE</version>
        </dependency>
    

    see https://github.com/paulvi/com.example.spring.findbyexample

    0 讨论(0)
  • 2020-12-02 21:06

    Spring data relies on top of JPA and EntityManager, not Hibernate and Session, and as such you do not have findByExample out of the box. You can use the spring data automatic query creation and write a method in your repository with the following signature:

    List<Person> findByEmployedAndLastNameAndDob(boolean employed, String lastName, LocalDate dob);
    
    0 讨论(0)
提交回复
热议问题