Java Spring Data JPA:示例查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27626825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Data JPA: Query by Example?
提问by Brice Roncace
Using Spring Data JPA can I do a query by examplewhere a particular entity instance is used as the search criteria?
使用 Spring Data JPA 是否可以按示例进行查询,其中将特定实体实例用作搜索条件?
For example (no pun intended), if I have a Person
entity that looks like:
例如(没有双关语),如果我有一个Person
看起来像的实体:
@Entity
public class Person {
private String firstName;
private String lastName;
private boolean employed;
private LocalDate dob;
...
}
I could find all employed persons with a last name of Smith born on January 1, 1977 with an example:
我可以找到所有姓氏为 Smith 且出生于 1977 年 1 月 1 日的雇员,例如:
Person example = new Person();
example.setEmployed(true);
example.setLastName("Smith");
example.setDob(LocalDate.of(1977, Month.JANUARY, 1));
List<Person> foundPersons = personRepository.findByExample(example);
采纳答案by David Rabinowitz
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:
Spring 数据依赖于 JPA 和 EntityManager,而不是 Hibernate 和 Session,因此您没有开箱即用的 findByExample。您可以使用 spring 数据自动查询创建并使用以下签名在您的存储库中编写一个方法:
List<Person> findByEmployedAndLastNameAndDob(boolean employed, String lastName, LocalDate dob);
回答by Brice Roncace
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
:
使用 Spring 数据的Specification
接口,我能够通过示例来近似使用查询。这是一个PersonSpec
实现Specification
并需要一个“示例”人员的类,以便设置由Predicate
返回的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);
回答by Adam Erstelle
This is now possible with Spring Data. Check out http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example
这现在可以通过 Spring Data 实现。查看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
请注意,这需要最新的 2016 版本
<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