Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring数据JPA中为自定义@Query提供和使用varargs或Iterable参数?_Java_Spring Data Jpa - Fatal编程技术网

Java 如何在Spring数据JPA中为自定义@Query提供和使用varargs或Iterable参数?

Java 如何在Spring数据JPA中为自定义@Query提供和使用varargs或Iterable参数?,java,spring-data-jpa,Java,Spring Data Jpa,我使用SpringDataJPA(目前为1.11.3.RELEASE)和Hibernate(目前为5.2.10.Final)作为JPA实现 我将以下场景作为(简化)示例,首先是一个简单的用户实体类: @Entity public class User { @Id private Long id; private boolean active; // getters/setters (not shown) } 使用基于CRUD的用户存储库。此用户存储库应提供停用由其id标识的用户的方法:

我使用SpringDataJPA(目前为1.11.3.RELEASE)和Hibernate(目前为5.2.10.Final)作为JPA实现

我将以下场景作为(简化)示例,首先是一个简单的用户实体类:

@Entity
public class User {

@Id
private Long id;

private boolean active;

// getters/setters (not shown)

}
使用基于CRUD的用户存储库。此用户存储库应提供停用由其id标识的用户的方法:

public interface UserRepository extends CrudRepository<User, Long> {

// deactivate single user
@Modifying
@Query("UPDATE User u SET active = false WHERE u.id = ?1")
void deactivate(Long id);

// TODO how to provide a deactivateAll method
}
public interface UserRepository扩展了crudepository{
//停用单个用户
@修改
@查询(“更新用户u SET active=false,其中u.id=?1”)
无效停用(长id);
//TODO如何提供一个deactivateAll方法
}
SpringDataJPA文档中使用详细描述了此解决方案


我在文档中遗漏了如何提供ID列表作为varags或Iterable参数,以允许同时停用多个用户。如何做到这一点?

在Spring Data JPA源代码中有一个类似的例子,用户存储库执行此操作:例如,使用@Query如下:

@Query("select u from User u where u.id in :ids")
Collection<User> findByIdsCustomWithNamedVarArgs(@Param("ids") Integer... ids);
@Query(“从用户u中选择u,其中u.id位于:id”)
集合findbyidscustomwithnamedvargs(@Param(“ids”)Integer…ids);
因此您可以使用varargs或Iterable提供ID列表,如下所示(在本例中使用命名参数变量):

修改 @查询(“更新用户u SET active=false,其中u.id位于:id”) 无效停用所有(@Param(“ids”)长…ids); @修改 @查询(“更新用户u SET active=false,其中u.id位于:id”) void deactivateAll(@Param(“ids”)Iterable ids);
@Modifying
@Query("UPDATE User u SET active = false WHERE u.id IN :ids")
void deactivateAll(@Param("ids") Long... ids);

@Modifying
@Query("UPDATE User u SET active = false WHERE u.id IN :ids")
void deactivateAll(@Param("ids") Iterable<Long> ids);