次に例を示します。
DetachedCriteria exampleSubquery = DetachedCriteria.forClass(MyPersistedObject.class)
.setProjection(Property.forName("id"))
// plus any other criteria...
;
Criteria criteria = getSession().createCriteria(ARelatedPersistedObject.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.add(Subqueries.propertyIn("myPersistedObjectId", exampleSubquery)));
複数のサブクエリの場合、Restrictions.or() のようなブール演算子を使用できます。
DetachedCriteria anotherSubquery = DetachedCriteria.forClass(MyPersistedObject.class)
.setProjection(Property.forName("id"))
// plus any other criteria...
;
Criteria criteria = getSession().createCriteria(ARelatedPersistedObject.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.add(Restrictions.or(
Subqueries.propertyIn("myPersistedObjectId", exampleSubquery),
Subqueries.propertyIn("myPersistedObjectId", anotherSubquery)));