Criteria子查询不为null

Mah*_*leh 9 hibernate detachedcriteria hibernate-criteria

我想将以下子查询转换为使用hibernate子查询:

getCurrentSession().createQuery("from Employee where id in (select adminId from Department where adminId is not null)")
                   .list();
Run Code Online (Sandbox Code Playgroud)

任何人都可以请给我一个这种转换的例子,因为我读了一些例子,我仍然无法弄清楚如何做到这一点.

JB *_*zet 21

Criteria c = session.createCriteria(Employee.class, "e");
DetachedCriteria dc = DetachedCriteria.forClass(Departemt.class, "d");
dc.add(Restrictions.isNotNull("d.adminId");
dc.setProjection(Projections.property("d.adminId"));
c.add(Subqueries.propertyIn("e.id", dc));
Run Code Online (Sandbox Code Playgroud)

setProjection调用使子查询adminId仅返回属性而不是整个Department实体.将Subqueries.propertyIn创建一个限制:属性id搜索的员工必须是in一组由子查询返回的结果.