[英]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: 带有ids的列表不为null,也不为空,但我总是得到以下异常:
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. 我很确定它与它所定义的IN子句的方式有关。
Any suggestions are well welcomed! 任何建议都很受欢迎!
It's a common issue, a bug in Hibernate
which you can find at : 这是一个常见问题,
Hibernate
中的一个错误,您可以在以下位置找到:
NullPointer when combining JPQL query with in clause and @NamedEntityGraph where it says that: 将JPQL查询与in子句和@NamedEntityGraph结合使用时的NullPointer ,它表示:
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.
当您将JPQL查询与in子句和@NamedEntityGraph结合使用时,执行查询时会在org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67)中获得NullPointerException。
So you would have to remove the @EntityGraph
annotation here in order to fix this problem. 所以你必须在这里删除
@EntityGraph
注释才能解决这个问题。
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 适用于JPA 2
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.