简体   繁体   English

JPA JPQL IN 子句:如何在 JPA 中使用 IN 子句?

[英]JPA JPQL IN clause: How to use IN clause in JPA?

@Query("SELECT al FROM Customer al WHERE al.companyCode = ?1 AND al.fileCode IN ?2")

List findallByGroup(int CompanyCode, String groups);列出 findallByGroup(int CompanyCode, String groups);

Or或者

@Query("SELECT al FROM Customer al WHERE al.companyCode = :CompanyCode AND al.fileCode IN :groups")

List<Customer> findallByGroup(@Param("CompanyCode") int CompanyCode,@Param("groups") List<BigInteger> groups);

OR或者

@Query("SELECT al FROM Customer al WHERE al.companyCode = :CompanyCode AND al.fileCode IN (:groups)")

List<Customer> findallByGroup(@Param("CompanyCode") int CompanyCode,@Param("groups") List<BigInteger> groups);
findAllByCompanyCodeAndFileCodeIn(int CompanyCode, List<String> groups)

You don't need @Query . 您不需要@Query Spring data can understand the query from method name. Spring数据可以从方法名称理解查询。 Use the above method. 使用以上方法。

Not directly related to OP's specific query, but relevant to JPA IN clause in general:与 OP 的特定查询没有直接关系,但通常与 JPA IN 子句相关:

Contrary to pvpkiran's answer, if you are doing anything other than SELECT queries (eg DELETE, UPDATE), it may be much more efficient to use @Query :与 pvpkiran 的回答相反,如果您正在执行 SELECT 查询以外的任何操作(例如 DELETE、UPDATE),则使用@Query可能更有效:

@Modifying
@Query("DELETE from Customer al where al.fileCode in :groups")
deleteByFileCodeIn(@Param("groups") List<String> groups)

instead of relying on Spring JPA's query method:而不是依赖 Spring JPA 的查询方法:

deleteByFileCodeIn(List<String> groups) // avoid this

Reason:原因:

Spring JPA query method's default implementation of the IN clause is inefficient. Spring JPA 查询方法对 IN 子句的默认实现是低效的。 Underneath the hood, it will 1. first select all records that match the IN criteria, then 2. execute a DELETE statement for each record found.在幕后,它将 1. 首先选择符合 IN 条件的所有记录,然后 2. 对找到的每个记录执行 DELETE 语句。

select customer.id as id,... from customer where customer.file_code in (?,?,?,...)
delete from customer where id=?
delete from customer where id=?
delete from customer where id=?
...

This means that if there are 1000 matching records, it will generate and execute 1000 delete statements -- instead of a single DELETE...IN statement which is what's usually intended.这意味着如果有 1000 条匹配的记录,它将生成并执行 1000 条删除语句——而不是通常预期的单个 DELETE...IN 语句。

By using @Query for IN clauses, you can override Spring JPA's default implementation and dictate a more efficient query to use instead.通过对 IN 子句使用@Query ,您可以覆盖 Spring JPA 的默认实现并指定一个更有效的查询来代替。 In my own testing this has resulted in 10x improvement in response time for large (>3K) datasets.在我自己的测试中,这导致大型(> 3K)数据集的响应时间提高了 10 倍。

One caveat is that depending on the database, there may be limitations in the number of parameters that can be used in the IN clause.一个警告是,根据数据库,可以在 IN 子句中使用的参数数量可能存在限制。 This can be overcome by partitioning the List.这可以通过对列表进行分区来克服。

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

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