First, this SQL query can be translated to HQL like this:
Code:
SELECT p.
FROM Parent p
JOIN p.childs c
WHERE c.id = (
SELECT max(c1.id) a
FROM Child c1
WHERE c1.parent = p
) AND c.age = 17;
For Criteria, try this query:
Code:
DetachedCriteria innerCriteria = DetachedCriteria.forClass(Child.class, "inner")
.add(Restrictions.eqProperty("inner.parent.id","upper.id"))
.setProjection(Projections.projectionList().add(Projections.max("inner.id")));
DetachedCriteria outerCriteria= DetachedCriteria.forClass(Parent.class, "upper");
outerCriteria.createAlias("upper.childs", "children");
outerCriteria.add(Subqueries.propertyEq("children.id", innerCriteria ));
outerCriteria.add(Restrictions.eqProperty("children.age", 17));
List<Parent> parents = outerCriteria.getExecutableCriteria(session).list();