I'm trying to build a Criteria query, but I'm having some difficulty.
In my system, a patient Case contains a Diagnostic, and each Diagnostic contains one or more MaladyDiagnostic. A MaladyDiagnostic can be any of several subclasses (let's call them MaladyDiagnosticA, MaladyDiagnosticB, and MaladyDiagnosticC, all of which extend MaladyDiagnostic).
The gist of my query is that I want to find all Cases that have a MaladyDiagnosticA in their Diagnostic's collection of MaladyDiagnostics.
Here's my (failed) attempt:
Code:
subQuery = DetachedCriteria.forClass(MaladyDiagnosticA.class);
subQuery.setProjection(Projections.id());
subQuery2 = DetachedCriteria.forClass(Diagnostic.class);
subQuery2.createAlias("maladyDiagnoses", "m");
subQuery2.add(Property.forName("m.id").in(subQuery));
subQuery2.setProjection(Projections.id());
c = session.createCriteria(Case.class);
c.createCriteria("diagnostic");
c.add(Property.forName("id").in(subQuery2));
In addition to probably being horribly inefficient, the above doesn't work. I always get zero rows returned, even though there are Cases in the test system that have a Diagnostic that contains a MaladyDiagnosisA. I'm quite new to Hibernate's Criteria API, and simply haven't been able to coax it to work. Any help would be greatly appreciated!