Spring Data JPA存储库:派生查询中的IN子句不起作用

jen*_*gar 6 java jpa spring-data spring-data-jpa

我有一个看起来像这样的存储库:

public interface UserRepository extends JpaRepository<User, Long>
{
    User findByEmailIgnoreCase(String email);

    @Query("select u from User u where u.id in (:ids)")
    Set<User> getByIdInSet(@Param("ids") Set<Long> ids);
}
Run Code Online (Sandbox Code Playgroud)

当我打电话时,getByIdInSet我收到以下错误:

Caused by: java.lang.IllegalArgumentException: You have attempted to set a value of type class org.eclipse.persistence.indirection.IndirectSet for parameter ids with expected type of class java.lang.Long from query string select u from User u where u.id in (:ids).
    at org.eclipse.persistence.internal.jpa.QueryImpl.setParameterInternal(QueryImpl.java:933)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:593)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
Run Code Online (Sandbox Code Playgroud)

显然我不知道是什么意思,因为我尝试更改各种数据类型,仍然得到相同的错误.我传递的id组只包含一个id.请指出我正确的方向.

Pre*_*ric 12

不用括号试试吧

@Query("select u from User u where u.id in :ids")
Run Code Online (Sandbox Code Playgroud)

  • [这里](http://stackoverflow.com/a/2793690/4074715)是关于此的另一篇文章,参考Hibernate bug以及参数周围所需的parens. (2认同)

att*_*ian 5

Spring Data JPA的最新版本支持IN关键字,该关键字可用于创建查询,如下所示:

Set<User> findByIdIn(Set<Long> ids);
Run Code Online (Sandbox Code Playgroud)

  • 此外,虽然 Set 是 IN 子句的最佳选择(以强制执行唯一性),但接受可变参数;`Set&lt;User&gt; findByIdIn(Long... ids);` (2认同)