Prashanth
Prashanth

Reputation: 31

Hibernate Criteria in Clause with 2 conditions in 1 Query

How do i acheive the following SQL Query using Hibernate Criteria Query:

select * from Table1 where (A,B) in (select A,B from Table2)

Upvotes: 1

Views: 1475

Answers (1)

Prashanth
Prashanth

Reputation: 31

Assuming we have Criteria for Table 1 and Detached Criteria for Table 2. This below code work flawlessly:

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));

Upvotes: 2

Related Questions