SQL中的Grails计算字段

SQL中的Grails计算字段,grails,gorm,Grails,Gorm,我有一个域名 class InvoiceLine { String itemName BigDecimal unitCost Integer quantity } } 我想使用.withCriteria生成一个grails闭包,该闭包对(unitCost*quantity)进行聚合,从而最终生成sql select item_name, sum(unit_cost * quantity) from invoice_line group by item_name; 现在,我能想出的最好办法就

我有一个域名

class InvoiceLine {

String itemName
BigDecimal unitCost
Integer quantity
}
}

我想使用.withCriteria生成一个grails闭包,该闭包对(unitCost*quantity)进行聚合,从而最终生成sql

select item_name, sum(unit_cost * quantity) from invoice_line group by item_name;
现在,我能想出的最好办法就是

def result = InvoiceLine.withCriteria {

        projections {
            groupProperty('itemName')
            sum ('quantity * unitCost')
        }
    }

不幸的是,当我运行上面的代码时,grails阻塞了。有人知道我怎样才能实现我的目标吗?非常感谢您的帮助。

是否需要条件查询?HQL在这里工作得很好:

def result = InvoiceLine.executeQuery(
  "select itemName, sum(unitCost * quantity) " + 
  "from InvoiceLine " +
  "group by itemName")
结果将是
对象[]
列表
,其中第一个元素是字符串(名称),第二个元素是数字(总和),因此,例如,您可以使用

results.each { row ->
   println "Total for ${row[0]} is ${row[1]}"
}

谢谢你,伯特。嗯,我想我可以用一种“纯粹的圣杯方式”。我猜HQL现在可能不得不这么做了。再次感谢。HQL via executeQuery的“纯Grails”不亚于criteria查询——这两种查询都包装了标准的底层Hibernate功能。做有效的事情。这里有一个好的答案