使用Hibernate Criteria转换内部查询

时间:2013-12-17 11:48:43

标签: java hibernate hibernate-criteria

我试图将嵌套查询转换为Hibernate Criteria,但无法做到。 实际上,尝试对结果集中的行进行计数和求和。

所以有人有任何想法吗?

提前致谢。

 SELECT DISTINCT host_platform,
      host_manufacturer,
      COUNT(phy_1),
      COUNT (vir_1),
      SUM (server_cost) AS server_cost,
      SUM (book_value) AS book_value,
      SUM (maintenance_cost) AS main_cost,
      SUM (current_cost) AS curr_cost 
 FROM (SELECT DISTINCT host_platform,
      host_manufacturer,
      phy_1,
      vir_1,
      server_cost,
      book_value,
      maintenance_cost,
      current_cost
         FROM tf_server_list_v
         where host_platform = 'UNIX')
                --ipaddress = '142.125.21.70')
         GROUP BY host_platform, host_manufacturer;

以下是为上述查询编写的标准

  1. 对于选择主查询,

    Criteria mainCriteria = getSession().createCriteria(TfServerListNew.Class);
    
    ProjectionList projOSList1 = Projections.projectionList();
    projOSList1.add(Projections.property("hostPlatform"), "hostPlatform");
    ProjectionList projectionList1 = Projections.projectionList();
    projectionList1.add(Projections.distinct(projOSList));
    projectionList1.add(Projections.alias(Projections.property("hostManufacturer"), "hostManufacturer"));
    projectionList1.add(Projections.alias(Projections.count("physicalFlag"), "physical"));
    projectionList1.add(Projections.alias(Projections.count("virtualFlag"), "virtual"));
    projectionList1.add(Projections.alias(Projections.sum("serverCost"), "serverCost"));
    projectionList1.add(Projections.alias(Projections.sum("bookValue"), "bookValue"));
    projectionList1.add(Projections.alias(Projections.sum("mainCost"), "mainCost"));
    projectionList1.add(Projections.alias(Projections.sum("currCost"), "currCost"));
    
    mainCriteria.setProjection(projectionList1);
    
  2. 对于内部查询来填充结果集并传递给主查询,

    final DetachedCriteria criteria = DetachedCriteria.forClass(TfServerListNew.class);
    ProjectionList projOSList = Projections.projectionList();
    projOSList.add(Projections.property("hostPlatform"), "hostPlatform");
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.distinct(projOSList));
    projectionList.add(Projections.alias(Projections.property("hostManufacturer"), "hostManufacturer"));
    projectionList.add(Projections.alias(Projections.property("physicalFlag"), "physical"));
    projectionList.add(Projections.alias(Projections.property("virtualFlag"), "virtual"));
    projectionList.add(Projections.alias(Projections.property("serverCost"), "serverCost"));
    projectionList.add(Projections.alias(Projections.property("bookValue"), "bookValue"));
    projectionList.add(Projections.alias(Projections.property("mainCost"), "mainCost"));
    projectionList.add(Projections.alias(Projections.property("currCost"), "currCost"));
    
  3. 但是如何将这个子查询传递给主查询却没有。

1 个答案:

答案 0 :(得分:0)

使用

mainCriteria.add(Restrictions.sqlRestriction("your nested query"));

或者你可以根据需要使用其他版本的sqlRestriction

我认为这种方法很好。或者你没有提供详细信息没有与你的代码相关的答案。