2014-09-26 50 views
0

我有以下SQL查詢:如何爲這種情況創建hibernate映射?

SELECT CONNECTIONDATE AS UNLOAD, 
    SUBSTR(ROUTECODE,1,(LENGTH(ROUTECODE)-2)) AS ROUTE, 
    COUNT(SUBSTR(ROUTECODE,1,(LENGTH(ROUTECODE)-2))) AS FREQUENCY 
    FROM RouteTableSynonym RC1 
    WHERE RC1.ROUTECONNECTIONTYPE = 'INBOUND' 
    and RC1.CONNECTIONTIME >= TO_TIMESTAMP('<sdate>', 'DD-MM-YYYY HH24:MI:SS') 
    and RC1.CONNECTIONTIME <= TO_TIMESTAMP('<edate>', 'DD-MM-YYYY HH24:MI:SS') 
    and RC1.LOGISTICSPOINTID in (SELECT DISTINCT RCO1.LOGISTICSPOINTID 
    FROM RouteOrderTableSynonym RCO1 
    WHERE RCO1.NAMC = '<namc>') 
    GROUP BY CONNECTIONDATE, SUBSTR(ROUTECODE,1,(LENGTH(ROUTECODE)-2)) 
    ORDER BY CONNECTIONDATE, ROUTE; 

由指定的所有值「< VAR_NAME>」被替換與被查詢的值。我也有存儲此查詢的結果這個實體類:

import java.util.Date; 

    public class DD_BlackoutRouteFrequencies { 

     private Date rte_day; 
     private String route; 
     private int freq; 
     private int delayedFreq; 

     public Date getRte_day() { 
      return rte_day; 
     } 
     public void setRte_day(Date rte_day) { 
      this.rte_day = rte_day; 
     } 
     public String getRoute() { 
      return route; 
     } 
     public void setRoute(String route) { 
      this.route = route; 
     } 
     public int getFreq() { 
      return freq; 
     } 
     public void setFreq(int freq) { 
      this.freq = freq; 
     } 
     public int getDelayedFreq() { 
      return delayedFreq; 
     } 
     public void setDelayedFreq(int delayedFreq) { 
      this.delayedFreq = delayedFreq; 
     } 
    } 

我執行這樣的查詢:

try{ 
     SQLQuery sqlquery = session.createSQLQuery(query); 
     sqlquery.addEntity(DD_BlackoutRouteFrequencies.class); 
     results = sqlquery.list(); 
     logger.debug(results.size()); 
     System.out.println("Frequnecy Results size: "+results.size()); 
    }catch(Exception e){ 
     logger.error("Exception ", e); 
     throw new RuntimeException("SQL Exception getting Blackout Route Frequencies: "+ e.getMessage()); 
    } 

我遇到的問題是,我無法弄清楚如何做該實體的hibernate映射接收此查詢的結果,該查詢從兩個不同的表中繪製其結果。

我必須爲查詢中使用的兩個表執行hibernate映射,然後映射實體嗎?

回答

0

您還可以獲取地圖的實例。例如:

SQLQuery sqlQuery = session.createSQLQuery(sql); 

// positional parameters (using ?) 
sqlQuery.setString(0, sdate); 
sqlQuery.setString(1, edate); 
sqlQuery.setString(2, namc); 

// scalar values for each column 
sqlQuery.addScalar("UNLOAD", Hibernate.STRING); 
sqlQuery.addScalar("ROUTE", Hibernate.STRING); 
sqlQuery.addScalar("FREQUENCY ", Hibernate.INTEGER); 

sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP); 
List<Map<String, Object>> list = sqlQuery.list(); 

for (Map<String, Object> map : list) { 
    System.out.println(map.get("UNLOAD")); 
    System.out.println(map.get("ROUTE")); 
    System.out.println(map.get("FREQUENCY")); 
} 
+0

這是工作謝謝 – JBMac 2014-09-26 20:01:40

相關問題