如何使用休眠条件查询实现以下SQL查询:
select * from Table1 where (A,B) in (select A,B from Table2)
最佳答案
假设我们有表1的条件和表2的分离条件。下面的代码可以正常工作:
Criteria criteria = new Criteria(Table1.class);
DetachedCriteria dc = DetachedCriteria.forClass(Table2.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("column1"));
projList.add(Projections.property("column2"));
dc.setProjection(Projections.distinct(projList));
criteria.add(Subqueries.propertiesIn(new String[]{"column1","column2"}, dc));