I found today a (strange) issue while I was attempting to fetch objects from database by their ids using Hibernate and JPQL.
The method I call is:
List<User> userList = userDao.findUsersByIds(ids);
In details:
@Repository
public interface UserDao extends JpaRepository<User, Long> {
(...)
@Query("SELECT u FROM User u " +
"WHERE u.id IN :ids")
List<User> findUsersByIds(@Param("ids") List<Long> ids);
}
I'm running out of ideas what the root cause ot this problem might be, since the list that's being passed is of type List<Long>
. It causes following stacktrace, though:
Caused by: java.lang.IllegalArgumentException: Parameter value element [37] did not match expected type [java.lang.Long (n/a)]
at org.hibernate.query.spi.QueryParameterBindingValidator.validateCollectionValuedParameterBinding(QueryParameterBindingValidator.java:77) ~[hibernate-core-5.3.3.Final.jar:5.3.3.Final]
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:46) ~[hibernate-core-5.3.3.Final.jar:5.3.3.Final]
at org.hibernate.query.spi.QueryParameterBindingValidator.validate(QueryParameterBindingValidator.java:27) ~[hibernate-core-5.3.3.Final.jar:5.3.3.Final]
at org.hibernate.query.internal.QueryParameterListBindingImpl.validate(QueryParameterListBindingImpl.java:75) ~[hibernate-core-5.3.3.Final.jar:5.3.3.Final]
at org.hibernate.query.internal.QueryParameterListBindingImpl.setBindValues(QueryParameterListBindingImpl.java:33) ~[hibernate-core-5.3.3.Final.jar:5.3.3.Final]
at org.hibernate.query.internal.AbstractProducedQuery.setParameterList(AbstractProducedQuery.java:568) ~[hibernate-core-5.3.3.Final.jar:5.3.3.Final]
at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:490) ~[hibernate-core-5.3.3.Final.jar:5.3.3.Final]
at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:106) ~[hibernate-core-5.3.3.Final.jar:5.3.3.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_144]
Try this (Query Method if you are using spring):
List<User> findAllByIdIn(List<Long> id);
Or JPQL:
@Query("SELECT u FROM User u WHERE u.id IN ?1")
List<User> findUsersByIds(List<Long> id);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.