使用Criteria API执行查询而不使用FROM语句

时间:2018-02-13 10:45:37

标签: java sql criteria-api

如何创建Criteria API查询,如

SELECT
  (select count(*) from table1),
  (select count(*) from table2),
  (select count(*) from table3)

注意:主查询中没有FROM语句

更新:我正在使用postgreSQL,我需要创建准确的Criteria API查询,例如:

    CriteriaBuilder criteriaBuilder = getSession().getCriteriaBuilder();
    CriteriaQuery<Long[]> mainQuery= criteriaBuilder.createQuery(Long[].class);

    Subquery<Long> subQuery1= mainQuery.subquery(Long.class);
    Root<Table1> subQuery1Root = subQuery1.from(Table1.class);
    subQuery1.select(criteriaBuilder.count(subQuery1Root));

    Subquery<Long> subQuery2= mainQuery.subquery(Long.class);
    Root<Table2> subQuery2Root = subQuery2.from(Table2.class);
    subQuery2.select(criteriaBuilder.count(subQuery2Root));

1 个答案:

答案 0 :(得分:0)

ANSI SQL替代方案:

SELECT *
from
  (select count(*) from table1) t1 cross join
  (select count(*) from table2) t2 cross join
  (select count(*) from table3) t3