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!
source to share
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.
source to share
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
source to share