简体   繁体   English

JPA:通过给定列的值列表获取实体列表

[英]JPA: Fetching list of entities by list of values for a given column

I am new to JPA and Hibernate.我是 JPA 和 Hibernate 的新手。

In one use case, I need to fetch all users from DB with associated email present in given email list.在一个用例中,我需要从给定 email 列表中存在关联 email 的数据库中获取所有用户。

To do this, I have written a custom JPA query as follows:为此,我编写了一个自定义 JPA 查询,如下所示:

    @Query("SELECT u from User u where u.email in :emailIds")
    List<User> findUsersByEmailIds(@Param("emailIds") List<String> emailIdList);

But, I would like to know, is there any better way to do the same?但是,我想知道,有没有更好的方法来做同样的事情?

As commented by Vishnu, here is the explanation:正如 Vishnu 评论的那样,这里是解释:

Assuming in your User entity you have a field like this:假设在您的用户实体中您有一个这样的字段:

private String email;

Then in your repository you can go like this:然后在您的存储库中,您可以像这样 go:

List<User> findByEmailIn(List<String> emailIdList);

The important key thing to notice here is the Capital E in findByEmailIn while in your entity this field was email (small e).这里要注意的重要关键是findByEmailIn中的大写字母Capital E ,而在您的实体中,该字段为email (小写字母 e)。 This way you can eliminate the @Query statement completely.这样您就可以完全消除@Query语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM