(Hibernate version: 3.0.5)
I have some problems using a subquery, first it cannot be
used with a property of an associated entity:
Criteria criteria = session.createCriteria(A.class);
criteria.add(...);
DetachedCriteria subQuery = DetachedCriteria.forClass(B.class);
subQuery.setProjection(Property.forName("B-some").max());
criteria.createCriteria("A-to-B-FK")
.add(Property.forName("B-some").eq(subQuery));
This code runs into a ClassCastException:
in SubqueryExpression.toSqlString(), line 43:
final SessionImpl session = ( (CriteriaImpl) criteria ).getSession();
criteria is cast to CriteriaImpl, but is of type CriteriaImpl.Subcriteria
Now, this really is a bug, or is it?
Furthermore, the subquery itself seems not to allow associations.
If I use the subquery in the main criteria (for testing to avoid
the above error), a simple flat subquery works. But if I make
an association:
subQuery.createCriteria("B-to-C-FK").add(Restrictions.eq("C-some",..));
the subquery-SQL is generated without the C-table, and uses
an alias for the C-table that was never defined:
.. where .. and x = [now the subquery:]
(select .. from B b_alias where c_alias.some = ..)
If I execute the subQuery (DetachedCriteria) directly it works
correctly. So again, the problem seems to be with subqueries.
At the moment I get the impression that Criteria API is not
usable for somewhat more complex queries - mine has a
subquery, multiple joins in outer query and subquery, and
uses properties of the outer query within the subquery.
Any help is appreciated.
|