SubqueryExpression throws ClassCastException on DetachedCriteria subqueries

Description

The toSqlString() method in SubqueryExpression contains a line that casts a Criteria object to CriteriaImpl in order to call the getSession() method. However, if DetachedCriteria is used as a subquery in a Criteria query, the underlying Criteria object will be CriteriaImpl.Subcriteria, thus a ClassCastException will be thrown.

I have created a bug fix. Add the following method to SubqueryExpression:

private SessionImpl getSessionImpl(Criteria criteria) {
SessionImpl session = null;
if (criteria instanceof CriteriaImpl) {
CriteriaImpl impl = (CriteriaImpl)criteria;
session = impl.getSession();
}
else if (criteria instanceof CriteriaImpl.Subcriteria){
CriteriaImpl.Subcriteria sub = (CriteriaImpl.Subcriteria)criteria;
//Alert! Recursive call here!
session = getSessionImpl(sub.getParent());
}
return session;
}

And then replace the offending line in toSqlString() with a call to the new method.

Attached is SubqueryExpression with proposed changes in place.

Environment

Oracle 9i, but probably not DB specific.

Activity

Show:
Thomas Goorden
November 29, 2006, 6:56 PM

This is slightly more elegant patch, that adds a getSession() method to the Criteria interface and implements it in Subcriteria.

RobR
September 1, 2007, 12:02 AM

This bug is still present as of 3.2.2 although the SubqueryExpression class has been completely rewritten. The offending line even as the word "ugly" appended to it as a comment.

I've attached a new patch that works with 3.2.2.

RobR
September 1, 2007, 12:04 AM

Patches class cast exception in 3.2.2 version of SubqueryExpression.

Gail Badner
May 2, 2008, 12:16 AM

This appears to have been fixed in 3.2.6 by HHH-952.

Steve Ebersole
March 21, 2011, 7:05 PM

Bulk closing stale resolved issues

Assignee

Gail Badner

Reporter

RobR

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Minor
Configure