how to perform union clause query with hibernate criteria api

匿名 (未验证) 提交于 2019-12-03 02:27:02

问题:

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.

回答1:

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. 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!