Disclaimer: I'm no EclipseLink specialist. My JPA experience to date has been with Hibernate only and the snippet below or the rest of the code is not using any vendor specific annotations. It is from a JPA 2.0 project.
Migrating from Hibernate 4.2.6 to EclipseLink 2.4.2 (or 2.5.1) for a project, many criteria queries that used to run correctly now abruptly break up with EclipseLink saying, for instance:
Exception Description: Invalid query key [
Query Key posts Base com.comax.model.mailing.Post]
in expression.Query: ReportQuery(referenceClass=Post )
where Point entity has a unidirectional 1-N relationship to Post.
The query is
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Post> q = cb.createQuery(Post.class);
Root<Point> root = q.from(Point.class);
Join<Point, Post> p1 = root.join("posts", JoinType.INNER);
Path<String> city = p1.get("city");
q.select(p1).distinct(true).where(cb.like(city, "L%"));
TypedQuery<Post> tq = em.createQuery(q);
List<Post> results = tq.getResultList();
i.e., selecting distinct `Post` entities whose `city` (String) attribute begins with `L` and navigating from the `Point` root.
The error message leaves me perplex. These Post and Point entities both inherit (JOINED strategy) from a common Base entity that is mentioned in Query Key posts Base. But I don't think that this is the issue. I tried querying an attribute that doesn't exist, like
and EclipseLink gives a totally different error.
Moreover, the JPQL equivalent
List<Post> results = em.createQuery(
"SELECT DISTINCT post FROM Point p JOIN p.posts post WHERE post.city LIKE 'L%' ORDER BY post.city",
Post.class).getResultList();
runs as expected.
How do I achieve this? Or am I missing something? Is there something specific to EclipseLink when it comes to criteria queries like setting a property in persistence.xml?
I read the exception description about query keys but I don't see why (or where) I'm supposed to create one.
Any help here would be most appreciated. Thank you folks.