具有参数属性的 Spring 数据 JPA 查询 [英] Spring data JPA query with parameter properties
问题描述
声明使用输入参数的属性作为查询参数的 Spring 数据 JPA 查询的最简单方法是什么?
What is the simplest way of declaring a Spring data JPA query that uses properties of an input parameter as query parameters?
例如,假设我有一个实体类:
For example, suppose I have an entity class:
public class Person {
@Id
private long id;
@Column
private String forename;
@Column
private String surname;
}
和另一个班级:
public class Name {
private String forename;
private String surname;
[constructor and getters]
}
...那我想写一个Spring数据仓库如下:
... then I would like to write a Spring data repository as follows:
public interface PersonRepository extends CrudRepository<Person, Long> {
@Query("select p from Person p where p.forename = ?1.forename and p.surname = ?1.surname")
findByName(Name name);
}
...但是 Spring data/JPA 不喜欢我在 ?1
参数上指定属性名称.
... but Spring data / JPA doesn't like me specifying property names on the ?1
parameter.
最好的选择是什么?
推荐答案
此链接将为您提供帮助:支持 SpEL 表达式的 Spring Data JPA M1.类似的例子是:
This link will help you: Spring Data JPA M1 with SpEL expressions supported. The similar example would be:
@Query("select u from User u where u.firstname = :#{#customer.firstname}")
List<User> findUsersByCustomersFirstname(@Param("customer") Customer customer);
https://spring.io/blog/2014/07/15/spel-support-in-spring-data-jpa-query-definitions
这篇关于具有参数属性的 Spring 数据 JPA 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!