4

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:

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 pretty sure it has something to do with the way the IN clause it's defined.

Any suggestions are well welcomed!

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
RaresI
  • 461
  • 1
  • 6
  • 19
  • because you are using a positional parameter (?1) rather than named (:param), and something is expecting named? – Neil Stockton Jul 16 '15 at 09:40
  • I have tried the following approach too, but I get the same NullPointerException: `@Query("select cs from CenterStudy cs where cs.id in (:ids)") @EntityGraph(value = "centerStudyAndNotifiedUsers", type = EntityGraph.EntityGraphType.LOAD) List findAllByIdsWithCenterAndUsers(@Param("ids") List ids);` – RaresI Jul 16 '15 at 09:42

3 Answers3

3

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.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
1
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

Piyush Mittal
  • 1,860
  • 1
  • 21
  • 39
1

I've faced this issue before. I fixed it using named parameteres 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
danilobraga
  • 165
  • 1
  • 9