2013-12-17 61 views
0

我想轉換成以下嵌套查詢到Hibernate標準,但無法做到。 實際上,試圖對結果集中的行進行計數和求和。使用休眠條件轉換內部查詢

有人有什麼想法嗎?

在此先感謝。這裏下面

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

但是如何將這個子查詢傳遞給主查詢沒有得到。

+0

到目前爲止你在哪裏?任何代碼?錯誤?所以我們可以幫助你更好。 –

回答

0

使用

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

,或者您可以使用sqlRestriction的其他版本的基礎上要求

我認爲這種方法是因爲你沒有提供的信息沒有回答關係到你的代碼好one.Or。