EclipseLink - Query failed to prepare, unexpected error occurred

泄露秘密 提交于 2019-12-11 05:51:37

问题


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


回答1:


I think the issue occurs when EclipseLink is trying to process countries matching done against collection:

.map(status -> cb.equal(root.get(MovieEntity_.countries), *countries*))

I think your intent was to check for equality with streamed item

.map(status -> cb.equal(root.get(MovieEntity_.countries), status))



回答2:


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.



来源:https://stackoverflow.com/questions/46748527/eclipselink-query-failed-to-prepare-unexpected-error-occurred

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!