[英]Fetching a list of parent entities with a filtered collection of children in Spring Data JPA
[英]JPA: Fetching list of entities by list of values for a given column
我是 JPA 和 Hibernate 的新手。
在一个用例中,我需要从给定 email 列表中存在关联 email 的数据库中获取所有用户。
为此,我编写了一个自定义 JPA 查询,如下所示:
@Query("SELECT u from User u where u.email in :emailIds")
List<User> findUsersByEmailIds(@Param("emailIds") List<String> emailIdList);
但是,我想知道,有没有更好的方法来做同样的事情?
正如 Vishnu 评论的那样,这里是解释:
假设在您的用户实体中您有一个这样的字段:
private String email;
然后在您的存储库中,您可以像这样 go:
List<User> findByEmailIn(List<String> emailIdList);
这里要注意的重要关键是findByEmailIn
中的大写字母Capital E
,而在您的实体中,该字段为email
(小写字母 e)。 这样您就可以完全消除@Query
语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.