[英]EclipseLink - Query failed to prepare, unexpected error occurred
執行命令時,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
我認為當EclipseLink嘗試處理與集合匹配的countries
匹配時,就會出現此問題:
.map(status -> cb.equal(root.get(MovieEntity_.countries), *countries*))
我認為您的意圖是檢查流項目是否相等
.map(status -> cb.equal(root.get(MovieEntity_.countries), status))
假設你希望所有MovieEntities
有一個或多個提供的Countries
,你可以嘗試更換cb.equal(root.get(MovieEntity_.countries), countries)
與cb.isMember(status, MovieEntity_.countries)
但是,這可能不是執行此操作的最佳方法,但至少最容易針對當前代碼進行測試。 也許您還應該將status
更改為country
以使代碼更易於理解。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.