The subquery is very useful in cases, that you need to search the User, having one-to-many UserDomains. In that case, the WHERE UserId IN (subquery)
brings big advanatage: we are still working with a flat User
table/entity... so we can do the proper paging.
Here is the documentation 15.8. Detached queries and subqueries
The draft could be: subquery:
DetachedCriteria userSubquery = DetachedCriteria.forClass(UserDomain.class, "ud")
// Filter the Subquery
.add(Restrictions.eq(UserDomain.DOMAIN, domain))
// SELECT The User Id
.setProjection( Projections.property("ud.userId") );
And the main query:
Criteria query = session.createCriteria(User.class, "u")
.add( Subqueries.propertyIn("u.id", userSubquery) );
Now we have a query, which could be used for paging
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…