执行命令时,EntityManager会抛出(所有https://pastebin.com/zKYBhsv8)
[EL Warning]: 2017-10-14 20:07:55.332--UnitOfWork(402037615)--Exception [EclipseLink-6168] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): org.eclipse.persistence.exceptions.QueryException
Exception Description: Query failed to prepare, unexpected error occurred: [java.lang.NullPointerException].
Internal Exception: java.lang.NullPointerException
Query: ReportQuery(referenceClass=MovieEntity )
代码看起来像这样
我创建
final CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
final CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
final Root<MovieEntity> root = countQuery.from(MovieEntity.class);
然后我创建谓词
final Predicate whereClause = MovieSpecs
.getFindPredicate(root, cb, countries);
这是方法
public static Predicate getFindPredicate(
final Root<MovieEntity> root,
final CriteriaBuilder cb
final List<CountryType> countries
) {
final List<Predicate> predicates = new ArrayList<>();
if(countries != null && !countries.isEmpty()) {
final List<Predicate> orPredicates =
countries
.stream()
.map(status -> cb.equal(root.get(MovieEntity_.countries), countries))
.collect(Collectors.toList());
predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
}
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
}
然后在countQuery中设置谓词
countQuery.select(cb.count(root)).where(whereClause);
并执行命令
final Long count = this.entityManager.createQuery(countQuery).getSingleResult();
在这里我抛出了上述错误。
MovieEntity:https://pastebin.com/CvhEQFZD MovieEntity_:http s://pastebin.com/ZyJL0nmM
答案 0 :(得分:0)
我认为当EclipseLink尝试处理针对集合完成的countries
匹配时会出现问题:
.map(status -> cb.equal(root.get(MovieEntity_.countries), *countries*))
我认为您的目的是检查与流式项目的相等性
.map(status -> cb.equal(root.get(MovieEntity_.countries), status))
答案 1 :(得分:0)
假设您希望所有MovieEntities
个人拥有一个或多个提供的Countries
,您可以尝试将cb.equal(root.get(MovieEntity_.countries), countries)
替换为cb.isMember(status, MovieEntity_.countries)
。
然而,这可能不是最佳方式,但至少最容易测试您当前的代码。也许您还应该将status
更改为country
,以使代码更容易理解。