Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 341077

Summary: CriteriaBuilder FunctionExpression (sum, diff...) Invalid query key in expression Exception
Product: z_Archived Reporter: Marc Nuri <marc>
Component: EclipselinkAssignee: Project Inbox <eclipselink.orm-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P2 CC: knut.wannheden, pihentagy+eclipse+bugzilla, terencekent, tom.ware, wismar25
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows Vista   
Whiteboard:

Description Marc Nuri CLA 2011-03-28 05:36:46 EDT
Whenever you combine attributes/expressions from different Roots in a CriteriaQuery an exception is thrown when running the query.
The problem is that EclipseLink transforms Criteria Expressions into EclipseLink Expressions. EclipseLink by design only supports compound expressions on the same table. 
When the expressions are combined using FunctionalExpressionImpl in the CriteriaBuilderImpl, the new expression sets as referenceClass for both expressions the class of the first expression's Root.
Later on when the query is run and the expression is normalized, an exception is thrown.

Exception Description: Invalid query key [attrNN] in expression. 
Query: ReportQuery(referenceClass=className)
Comment 1 Tom Ware CLA 2011-04-06 08:27:20 EDT
Please provide example code that shows the problem.  Without additional information we will have to close this bug because we will not be able to recreate it.

Setting initial target and priority.  See the following page for the meanings of these fields:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines

Community: Please vote for this bug if it is important to you.  Votes are one of the main criteria we use to determine which bugs to fix next.
Comment 2 pihentagy+eclipse+bugzilla CLA 2011-04-20 11:04:45 EDT
I have a similar problem (is this the same?). (2.1.2.v20101206-r8635)
Given the following query, it just runs fine in HSQLDB, but failes in postgres 9:
(note, that bot Posting and Closing is a descendant of Resource)

		final CriteriaBuilder cb = em.getCriteriaBuilder();
		final CriteriaQuery<Long> c = cb.createQuery(Long.class);
		final Root<Posting> root = c.from(Posting.class);
		final Root<Closing> cl = c.from(Closing.class);
		c.select(cl.get(Closing_.closingRequest).get(Resource_.id));
		//c.distinct(true);

		c.where( //
		cb.and(cb.equal(root.get(Posting_.closingId), cl.get(Resource_.id)), //
				cb.notEqual(cl.get(Closing_.closingRequest).get(AbstractRequest_.requestState),
						RequestState.COMPLETED)));
		final List<Long> resultList = em.createQuery(c).getResultList();

Trace:
Exception Description: Invalid query key [closingId] in expression.
Query: ReportQuery(referenceClass=Closing )
	at org.eclipse.persistence.exceptions.QueryException.invalidQueryKeyInExpression(QueryException.java:638)
	at org.eclipse.persistence.internal.expressions.QueryKeyExpression.validateNode(QueryKeyExpression.java:785)
	at org.eclipse.persistence.expressions.Expression.normalize(Expression.java:2985)
	at org.eclipse.persistence.internal.expressions.DataExpression.normalize(DataExpression.java:392)
	at org.eclipse.persistence.internal.expressions.QueryKeyExpression.normalize(QueryKeyExpression.java:562)
	at org.eclipse.persistence.internal.expressions.QueryKeyExpression.normalize(QueryKeyExpression.java:549)
	at org.eclipse.persistence.internal.expressions.RelationExpression.normalize(RelationExpression.java:517)
	at org.eclipse.persistence.internal.expressions.CompoundExpression.normalize(CompoundExpression.java:218)
	at org.eclipse.persistence.internal.expressions.CompoundExpression.normalize(CompoundExpression.java:218)
	at org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalize(SQLSelectStatement.java:1306)
	at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.buildReportQuerySelectStatement(ExpressionQueryMechanism.java:551)
	at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.buildReportQuerySelectStatement(ExpressionQueryMechanism.java:497)
	at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.prepareReportQuerySelectAllRows(ExpressionQueryMechanism.java:1527)
	at org.eclipse.persistence.queries.ReportQuery.prepareSelectAllRows(ReportQuery.java:1276)
	at org.eclipse.persistence.queries.ReadAllQuery.prepare(ReadAllQuery.java:734)
	at org.eclipse.persistence.queries.ReportQuery.prepare(ReportQuery.java:1038)
	at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:464)
	at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:732)
	at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:430)
	at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:646)
	at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:958)
	at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:432)
	at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1021)
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2857)
	at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1225)
	at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1207)
	at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1181)
	at org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:453)
	at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getResultList(EJBQueryImpl.java:681)
	at sun.reflect.GeneratedMethodAccessor323.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:310)
	at $Proxy739.getResultList(Unknown Source)
[...]
Comment 3 Wilber Saca CLA 2012-11-15 15:23:05 EST
any solution or workaround for this problem?

This line is causing a exception:
Expression<String> e1 = cb.upper(cb.concat(" ", fromAlu.get(ClienteLocal_.cllcNmb)));

but this is working:
Expression<String> e1 = cb.upper(fromAlu.get(ClienteLocal_.cllcNmb));

when I use the concat function I get this exeption:
Caused by: Exception [EclipseLink-6015] (Eclipse Persistence Services - 2.5.0.v20121114-e97b8b7): org.eclipse.persistence.exceptions.QueryException
Exception Description: Invalid query key [cllcNmb] in expression.
Query: ReadAllQuery(referenceClass=ClienteLocal )
	at org.eclipse.persistence.exceptions.QueryException.invalidQueryKeyInExpression(QueryException.java:687)
	at org.eclipse.persistence.internal.expressions.QueryKeyExpression.validateNode(QueryKeyExpression.java:905)
	at org.eclipse.persistence.expressions.Expression.normalize(Expression.java:3259)
...

I'm using two tables without any direct relationship.
Comment 4 Tom Ware CLA 2012-11-16 10:46:06 EST
Have you tried to workaround using JPQL?
Comment 5 Wilber Saca CLA 2012-11-19 08:47:58 EST
I was using JPQL, but I have changed to Criteria because I need construct dynamic queries for my reports (each day more with different requirements).
Comment 6 Terence Kent CLA 2015-01-08 23:58:55 EST
I'm in the same situation as William, I need to use the Criteria API to build a dynamic query and this is preventing that from being possible without ol'fashioned string concatenation. 

Would be great to have this resolved.
Comment 7 Eclipse Webmaster CLA 2022-06-09 10:09:49 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink