2011-08-11 36 views
2

http://jira.grails.org/browse/GRAILS-2803由於缺少對sqlProjection的支持。它作爲一個重複被封閉,據說在Grails v1.2中被修復。但是,「重複」問題涉及到sqlRestriction,我不認爲sqlProjection至少已經在Grails v1.3.2中實現。解決標準構建器中缺少sqlProjection支持的問題?

是否有任何解決方法在critera中使用sqlprojection?例如,

def results = Report.createCriteria() list { 
      projections { 
       sum('correctResponses') 
       sum('allResponses') 

       sqlProjection("(sum(correct_responses)/sum(all_responses))", ["grade"] as String[], [Hibernate.INTEGER] as Type[]) 
       groupProperty('name') 
      } 
      and { 
       if (startDate) 
        ge("date", startDate) 

       if (endDate) 
        lt("date", endDate + 1) //add one day so search is inclusive of end date 

       'in' ("id", ids) 
      } 
      order(orderColumn ?: 'name', orderDirection ?: 'asc') 
     } 

回答

4

我有一個類似的問題,並找到了解決方案中的另一個問題的答案。

您可以使用addProjectionToList()方法解決此問題。在下面的答案爲例看看「選項2」:

https://stackoverflow.com/a/7652787

的標準DSL爲這個特殊的例子是如下:

def results = Report.createCriteria().list { 
    projections { 
     sum('correctResponses') 
     sum('allResponses') 

     addProjectionToList(Projections.sqlProjection(
      '(sum(correct_responses)/sum(all_responses)) as grade', 
      ['grade'] as String[], 
      [Hibernate.INTEGER] as Type[] 
     ), 'sumProjection') 
     groupProperty('name') 
    } 
    and { 
     if (startDate) 
      ge("date", startDate) 

     if (endDate) 
      lt("date", endDate + 1) //add one day so search is inclusive of end date 

     'in'("id", ids) 
    } 
    order(orderColumn ?: 'name', orderDirection ?: 'asc') 
} 
相關問題