You can use DetachedCriteria to create a subquery.
// This corresponds to (select firstname from contact where id = 1) DetachedCriteria subquery = DetachedCriteria.forClass(Contact.class) .add(Restrictions.eq("id", 1)) .setProjection(Projections.property("name")) // This corresponds to (select information where name not in (subquery)) ICriteria criteria = session .createCriteria(Information.class) .add(Subqueries.notIn("name", subquery));
Instead of using a subquery, you can simply load the contact using session.get, with the possibility of getting into the cache:
Contact contact = session.Get<Contact>(1); ICriteria criteria = session .createCriteria(Information.class) .add(Property.ne("name", contact.getName()));
Disclamer: I'm a) not a Java programmer and b) may have made mistakes, so it probably won't compile. The code more crudely demonstrates the idea and, hopefully, will be useful in any case.
Stefan steinegger
source share