Spring Data JPA:按示例查询? [英] Spring Data JPA: Query by Example?

查看:103
本文介绍了Spring Data JPA:按示例查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Spring Data JPA我可以做一个按示例查询,其中特定实体实例用作搜索条件?

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 Person entity that looks like:

@Entity
public class Person {
  private String firstName;
  private String lastName;
  private boolean employed;
  private LocalDate dob;
  ...
}

我可以找到所有受雇的人1977年1月1日出生的史密斯名称,例如:

I could find all employed persons with a last name of Smith born on January 1, 1977 with an example:

Person example = new Person();
example.setEmployed(true);
example.setLastName("Smith");
example.setDob(LocalDate.of(1977, Month.JANUARY, 1));
List<Person> foundPersons = personRepository.findByExample(example);


推荐答案

Spring数据依赖于JPA和EntityManager之上,而不是Hibernate和Session,因此你没有开箱即用的findByExample。您可以使用spring数据自动查询创建,并使用以下签名在存储库中编写方法:

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);

这篇关于Spring Data JPA:按示例查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆