Minesh
Minesh

Reputation: 166

how to perform union clause query with hibernate criteria api

SELECT 
    supplier_id  FROM suppliers   UNION ALL 

SELECT     supplier_id  FROM orders;

i just creating two criteria above "UNION ALL" clause of query and below "UNION ALL" clause of query.

but my question is how i perform UNION ALL clause in criteria? Thanks in Advance.

Upvotes: 5

Views: 21771

Answers (1)

void
void

Reputation: 7890

with criteria I think hibernate does not support UNION ALL but you can use two criteria queries to get the expected result:

Criteria cr1 = session.createCriteria(Suppliers.class);
 cr1.setProjection(Projections.projectionList()
    .add( Projections.property("supplier_id"), "supplier_id" )
 );
List results1 = cr1.list();

Criteria cr2 = session.createCriteria(Orders.class);
 cr2.setProjection(Projections.projectionList()
    .add( Projections.property("supplier_id"), "supplier_id" )
 );
List results2 = cr2.list();

results1.add(results2); 

List unionAllList =  results1; //this is the expected result.

Upvotes: 8

Related Questions