Java Spring Data JPA 存储库:派生查询中的 IN 子句不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26677310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Data JPA repositories: IN-clause in derived query not working
提问by jensengar
I have a repository that looks like this:
我有一个看起来像这样的存储库:
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);
}
When I call getByIdInSet
I get the following error:
当我打电话时,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)
Apparently I don't know what means because I have tried changing all sorts of data types and still get the same error. The set of ids I am passing contains just one id. Please point me in the right direction.
显然我不知道是什么意思,因为我已经尝试更改各种数据类型,但仍然出现相同的错误。我传递的一组 id 只包含一个 id。请指出我正确的方向。
采纳答案by Predrag Maric
Try it without the brackets
不带括号试试
@Query("select u from User u where u.id in :ids")
回答by attacomsian
Latest version of Spring Data JPA supports IN keyword which can be used to create the query like the following:
最新版本的 Spring Data JPA 支持 IN 关键字,可用于创建如下查询:
Set<User> findByIdIn(Set<Long> ids);