RaresI RaresI - 2 months ago 35
Java Question

How to use the IN clause in an JPA @Query annotation

I have defined this method

@Query("select cs from CenterStudy cs where cs.id in (?1)")
@EntityGraph(value = "centerStudyAndNotifiedUsers", type = EntityGraph.EntityGraphType.LOAD)
List<CenterStudy> findAllByIdsWithCenterAndUsers(List<Long> ids);

The list with the ids is not null, nor empty, but I always get the following exception:

at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:613)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1900)

I'm pretty sure it has something to do with the way the IN clause it's defined.

Any suggestions are well welcomed!


It's a common issue, a bug in Hibernate which you can find at :

NullPointer when combining JPQL query with in clause and @NamedEntityGraph where it says that:

When you combine a JPQL query with an in clause and a @NamedEntityGraph you get a NullPointerException in org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67) when you execute the query.

So you would have to remove the @EntityGraph annotation here in order to fix this problem.