How to use IN clause in 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 ids is not null or empty, but I always get the following exception:

java.lang.NullPointerException
    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 sure it has something to do with how it is defined in the IN clause.

Any suggestions are greatly appreciated!

+3


source to share


3 answers


This is a common bug Hibernate

, which you can find at:

NullPointer when combining JPQL query with clause and @NamedEntityGraph which says that:



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

So, you need to remove the annotation @EntityGraph

here to fix this issue.

+3


source


public List<DealInfo> getDealInfos(List<String> dealIds) {
        String queryStr = "SELECT NEW com.admin.entity.DealInfo(deal.url, deal.url, deal.url, deal.url, deal.price, deal.value) " + "FROM Deal AS deal where deal.id in :inclList";
        TypedQuery<DealInfo> query = em.createQuery(queryStr, DealInfo.class);
        query.setParameter("inclList", dealIds);
        return query.getResultList();
    }

      



Works with JPA 2

+1


source


I've encountered this problem before. I fixed it by using named parameters instead of positional parameters. Example:

"Select p from product where p.id in(?)" -- not working

      

replaced by:

"Select p from product where p.id in(:idList)" -- OK

      

0


source







All Articles