Spring Data JPA repositories: IN-clause in derived

2019-04-08 05:16发布

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:

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.

2条回答
Ridiculous、
2楼-- · 2019-04-08 06:07

Try it without the brackets

@Query("select u from User u where u.id in :ids")
查看更多
Deceive 欺骗
3楼-- · 2019-04-08 06:16

Latest version of Spring Data JPA supports IN keyword which can be used to create the query like following:

Set<User> findByIdIn(Set<Long> ids);
查看更多
登录 后发表回答