I'm trying to narrow the result set of Hibernate Criteria query by result of another query. I know how to resolve this problem with JPQL:
FROM DocPackage p WHERE
EXISTS (SELECT g
FROM ObjectGroup g JOIN g.items i, Person per
WHERE g=p.applicantGroup
AND i.objectClass = 'org.cp.model.common.Person'
AND i.objectId=per.id
AND lower(concat(per.firstName,' ',per.lastName)) like :applicant
)
But I can't imagine how to make such query with Criteria. Any ideas how to implement this selection with Criteria? Hibernate 3.3 is used.
UPD: Trying to solve this problem I've made the following Criteria query:
Criteria resultCriteriaQuery = this.hibernateSession.createCriteria(DocPackage.class, "pack");
DetachedCriteria personSubquery = DetachedCriteria.forClass(Person.class, "pers").
add(Restrictions.like("pers.loFstLstName", "%" + searchObject.getApplicant().toLowerCase() + "%")).
add(Restrictions.eqProperty("itm.objectId", "pers.id"));
DetachedCriteria applicantsSubquery = DetachedCriteria.forClass(ObjectGroup.class, "objGrp").
add(Restrictions.eqProperty("pack.applcantGroup", "objGrp")).
createAlias("objGrp.items", "itm").
add(Restrictions.eq("itm.objectClass", "org.cp.model.common.Person")).
add(Subqueries.exists(personSubquery));
resultCriteriaQuery.add(Subqueries.exists(applicantsSubquery));
But it doesn't work. I have a NullPointerException
on resultCriteriaQuery.list()
. What's wrong with this query? Any ideas?