testData.prepare();
Session session = openSession();
session.enableFilter("region").setParameter("region", "APAC");
DetachedCriteria salespersonSubquery = DetachedCriteria.forClass(Salesperson.class)
.add(Restrictions.eq("name", "steve"))
.setProjection(Property.forName("department"));
Criteria departmentsQuery = session.createCriteria(Department.class).add(Subqueries.propertyIn("id", salespersonSubquery));
List departments = departmentsQuery.list();
assertEquals("Incorrect department count", 1, departments.size());
session.enableFilter("region").setParameter("region", "Foobar");
departments = departmentsQuery.list();
assertEquals("Incorrect department count", 0, departments.size());
session.enableFilter("region").setParameter("region", "APAC");
DetachedCriteria lineItemSubquery = DetachedCriteria.forClass(LineItem.class)
.add(Restrictions.ge("quantity", new Long(1)))
.createCriteria("product")
.add(Restrictions.eq("name", "Acme Hair Gel"))
.setProjection(Property.forName("id"));
List orders = session.createCriteria(Order.class)
.add(Subqueries.exists(lineItemSubquery))
.add(Restrictions.eq("buyer", "gavin"))
.list();
assertEquals("Incorrect orders count", 1, orders.size());
session.enableFilter("region").setParameter("region", "APAC");
session.enableFilter("effectiveDate").setParameter("asOfDate", testData.lastMonth.getTime());
DetachedCriteria productSubquery = DetachedCriteria.forClass(Product.class)
.add(Restrictions.eq("name", "Acme Hair Gel"))
.setProjection(Property.forName("id"));
lineItemSubquery = DetachedCriteria.forClass(LineItem.class)
.add(Restrictions.ge("quantity", new Long(1)))