Spring Data JPA: Query by Example?(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?
例如(没有双关语),如果我有一个 Person
实体,看起来像:
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;
...
}
我可以找到所有姓氏为 Smith 出生于 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:通过示例查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Spring Data JPA:通过示例查询?


- Eclipse 插件更新错误日志在哪里? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01