【发布时间】:2018-03-26 16:31:36
【问题描述】:
执行命令时,EntityManager 抛出(all 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/CvhEQFZDMovieEntity_:http s://pastebin.com/ZyJL0nmM
【问题讨论】:
-
你能详细说明你想和这个
cb.equal(root.get(MovieEntity_.countries), countries)比较吗? -
现在我认为并看到它毫无意义。使用此代码,我想检查给定国家是否在电影国家列表中。你知道怎么做吗?
-
使用 'in' 检查一个元素是否在列表中,但您需要更具体一些,因为在列表中的两个国家/地区播放的电影将在您的结果中显示两次,原因是通过 OneToMany 关系加入。
标签: java spring jpa spring-boot eclipselink