问题描述
执行命令时,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
假定您希望所有具有一个或多个提供的Countries
的MovieEntities
,则可以尝试将cb.equal(root.get(MovieEntity_.countries), countries)
替换为cb.isMember(status, MovieEntity_.countries)
. /p>
但是,这可能不是执行此操作的最佳方法,但至少最容易针对您当前的代码进行测试.也许您还应该将status
更改为country
,以使代码更易于理解.
When executing a command, EntityManager throws out(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 )
The code looks like this
I create
final CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
final CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
final Root<MovieEntity> root = countQuery.from(MovieEntity.class);
then I create predicate
final Predicate whereClause = MovieSpecs
.getFindPredicate(root, cb, countries);
this is the method
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()]));
}
then set predicate in countQuery
countQuery.select(cb.count(root)).where(whereClause);
and execute the command
final Long count = this.entityManager.createQuery(countQuery).getSingleResult();
and here I am throwing the above errors.
MovieEntity: https://pastebin.com/CvhEQFZD MovieEntity_: http s://pastebin.com/ZyJL0nmM
Assuming that you want all MovieEntities
that have one or more of the provided Countries
you could try to replace cb.equal(root.get(MovieEntity_.countries), countries)
with cb.isMember(status, MovieEntity_.countries)
.
However this might not be the most optimal way to do this but at least easiest to test against your current code. Maybe you should also change status
to country
to make code more understandable.
这篇关于EclipseLink-查询准备失败,发生意外错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!