2012-01-13 50 views
0

嗨,我喜歡月智表jan,feb..dec和我有locatin和actioncount字段中每個表中重複的位置。問題與SQL查詢轉換爲冬眠查詢

我有這個查詢大致寫在SQL中,我有月域對象,現在我必須將其轉換爲Hibernate查詢(HQL或標準api或其他任何..)。我如何轉換它? 月的計數是作爲一個列表提供的,並且是如此的變量 下面的sql是從這個列表monthsToQuery = [oct,nov,dec,jan] ..這也是變量列表。 它可以是[二月,三月,四月]或[月]

select loc, sum(tcount) from (
        (select location as loc, sum(actioncount) as tcount from oct group by location) left-join 
        (select location as loc, sum(actioncount) as tcount from nov group by location) left-join 
        (select location as loc, sum(actioncount) as tcount from dec group by location) left-join 
        (select location as loc, sum(actioncount) as tcount from jan group by location) 

        ) group by loc 

我做左加入,因爲我不想失去不同月份之間的任何位置。

此外:我還有一個日期範圍作爲輸入。到目前爲止,我正在從範圍中獲得一個月的列表,並獲得每月分離的結果。我需要編寫查詢以在1個查詢中提供最終的所需結果。 這裏是我有什麼至今:

// sTblList - list of all month domains in the date range.. 
def getSummary(sTblList,SfromDate,StoDate,res_id, groupCol,sumCol){ 
     try{ 
     Date fromDate = new Date().parse("yyyy-MM-dd", SfromDate); 
     Date toDate = new Date().parse("yyyy-MM-dd", StoDate); 

     def resourceInstance=Resources.get(res_id); 
     sTblList.each{ 
     def OnemonthList=it.createCriteria().get {  
      eq('graresource',resourceInstance) 
      between('currentdate', fromDate, toDate)   
      projections { 
      sum(sumCol,'tcount') 
      groupProperty(groupCol)  
       }     
      }  

     return sumMap // sumMap should have all months results combined 
    } 

我看了一些地方,與其嵌套規定 - 我還可以在條件中使用別名。我是新來的.. 有沒有人知道更進一步?

+0

我會首先對模式進行規範化,然後製作一個包含月份列的表格。這樣一切都會容易得多。 – 2012-01-13 10:26:39

+0

我們已經將數據分爲12個表格以進行性能和總結,它是一個巨大的表格,否則有5000萬條記錄。 – 2012-01-13 23:12:32

+0

使用月份列索引時,它可能比加入所有這些錶快得多。對於一個好的數據庫來說,5000萬是沒有那麼多。 – 2012-01-14 09:00:57

回答

1

如果您的模型使用繼承

abstract class Month 
{ 
    string location; 
    int actionCount; 
} 

class January extends Month 
{ 
} 

session.CreateCriteria(Month.class) 
    .SetProjection(Projections.ProjectionList() 
     .Add(Projections.GroupProperty("Location")) 
     .Add(Projections.Sum("ActionCount"))) 

或接口

class Month implements HasActionCount 
{ 
    string location; 
    int actionCount; 
} 

session.CreateCriteria(HasActionCount.class) 
    .SetProjection(Projections.ProjectionList() 
     .Add(Projections.GroupProperty("Location")) 
     .Add(Projections.Sum("ActionCount"))) 

更新:爲NHibernate和SQLite的以下作品(也應該在Hibernate中工作)

class Month 
{ 
    public virtual int Id { get; set; } 
    public virtual string Location { get; set; } 
    public virtual int ActionCount { get; set; } 
} 

class January : Month 
{ 
} 

class February : Month 
{ 
} 

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> 
    <class xmlns="urn:nhibernate-mapping-2.2" name="ConsoleApplication1.January, ConsoleApplication1" table="`January`"> 
    <id name="Id"> 
     <generator class="identity" /> 
    </id> 
    <property name="Location" /> 
    <property name="ActionCount" /> 
    </class> 
    <class xmlns="urn:nhibernate-mapping-2.2" name="ConsoleApplication1.February, ConsoleApplication1" table="`February`"> 
    <id name="Id"> 
     <generator class="identity" /> 
    </id> 
    <property name="Location" /> 
    <property name="ActionCount" /> 
    </class> 
</hibernate-mapping> 

// works as expected 
IList<Month> months = session.CreateCriteria<Month>().List<Month>(); 

// returns the location and sum of each month though 
IList<Object[]> sums = (Object[])session.CreateCriteria<Month>() 
    .SetProjection(Projections.ProjectionList() 
     .Add(Projections.GroupProperty("Location")) 
     .Add(Projections.Sum("ActionCount"))) 
    .List(); 
+0

感謝您的提示..那麼月份的域名類將會存儲來自12個月度表的所有位置和行動計數是嗎?因爲12個表中的插入是通過在後臺運行的夜間作業完成的,所以即使對於這個基本域,插入過程也必須完成? – 2012-01-13 23:10:23

+0

你必須獨立映射所有月份,但hibernate足夠聰明,知道當你查詢基類時,它必須查詢所有繼承的類,並獲得一個繼承類(1月,...)的列表。生成的SQL應該看起來像你的查詢。接口也一樣,我不確定接口是否需要映射,可能不是。 – Firo 2012-01-14 22:51:57

+0

我也剛剛在一些相關的帖子上看到,課堂月份需要抽象嗎? – 2012-01-15 18:16:04