2013-08-19 101 views
1

我需要在Grails中使用GROUP_CONCAT聚合函數,最好來自HQL,但也符合條件。如何在Grails中使用GROUP_CONCAT與Gorm

我有這個疑問:

ClickTracking.executeQuery("SELECT pageId, containerId, GROUP_CONCAT(clicks) as click" + 
              "FROM ClickTracking " + 
              "WHERE pageId = ? " + 
              "GROUP BY containerId ", [pageId]) 

這不是工作,因爲HQL是不知道有關GROUP_CONCAT因爲是DB具體。我確定我的領帶項目與MySQL,所以我試圖在BootStrap.groovy中添加此:

Configuration conf = grailsApplication.getMainContext().getBean("&sessionFactory").configuration; 
conf.addSqlFunction("GROUP_CONCAT", new StandardSQLFunction("GROUP_CONCAT", new StringType())); 

沒有運氣。

然後我試圖子類的話,並使用它:

import org.hibernate.dialect.MySQL5InnoDBDialect 
import org.hibernate.dialect.function.StandardSQLFunction 
import org.hibernate.Hibernate 

class ExtendedMySqlDialect extends MySQL5InnoDBDialect { 

public ExtendedMySqlDialect() { 
    super(); 
    registerFunction("GROUP_CONCAT", new StandardSQLFunction("GROUP_CONCAT", Hibernate.STRING)); 

} 

}

和DataSource.groovy中

dataSource { 
pooled = true 
driverClassName = "com.mysql.jdbc.Driver" 
dialect = "ExtendedMySqlDialect" 
logSql = true 

}

仍然沒有運氣。我得到:

No data type for node: org.hibernate.hql.ast.tree.MethodNode 

- [METHOD_CALL] MethodNode: '(' + - [METHOD_NAME] IdentNode: 'GROUP_CONCAT'{originalText = GROUP_CONCAT} - [一個expr_list] SqlNode: 'exprList' - [點] DotNode:'clicktrack0_.clicks'{propertyName = clicks,dereferenceType = ALL,propertyPath = clicks,path = {synthetic-alias} .clicks,tableAlias = clicktrack0_,className = com.ui.gorm.ClickTracking,classAlias = null} + - [IDENT] IdentNode:'{synthetic-alias}'{originalText = {synthetic-alias}} - [IDENT] IdentNode:'clicks'{originalText = clicks} 。Stacktrace如下: 消息:節點沒有數據類型:org.hibernate.hql.ast.tree。 MethodNode - [METHOD_CALL] MethodNode: '(' + - [METHOD_NAME] IdentNode: 'GROUP_CONCAT'{originalText = GROUP_CONCAT} - [一個expr_list] SqlNode: 'exprList' - [點] DotNode: 'clicktrack0_.clicks'{ propertyName = clicks,dereferenceType = ALL,propertyPath = clicks,path = {synthetic-alias} .clicks,tableAlias = clicktrack0_,className = com.ui.gorm.ClickTracking,classAlias = null} + - [IDENT] IdentNode:'{合成別名}」 {originalText = {合成別名}} - [IDENT] IdentNode: '點擊次數'{originalText =點擊}

Line | Method 

- >> 156 | initializeExplicitSelectClause在org.hibernate.hql.ast.tree.SelectClause

如果我設置一個斷點,我期待在grailsApplication.getMainContext()。的getBean(「& SessionFactory的」)。配置我能找到一個有屬性命名sqlFuncions和GROUP_COCNAT在那裏。

我做了一些調試和我結束了在SelectExpressionList.java在此代碼:

public SelectExpression[] collectSelectExpressions() { 
    // Get the first child to be considered. Sub-classes may do this differently in order to skip nodes that 
    // are not select expressions (e.g. DISTINCT). 
    AST firstChild = getFirstSelectExpression(); 
    AST parent = this; 
    ArrayList list = new ArrayList(parent.getNumberOfChildren()); 
    for (AST n = firstChild; n != null; n = n.getNextSibling()) { 
     if (n instanceof SelectExpression) { 
      list.add(n); 
     } 
     else { 
      throw new IllegalStateException("Unexpected AST: " + n.getClass().getName() + " " + new ASTPrinter(SqlTokenTypes.class).showAsString(n, "")); 
     } 
    } 
    return (SelectExpression[]) list.toArray(new SelectExpression[list.size()]); 
} 

看來,N = n.getNextSibbling()以某種方式搞亂與GROUP_CONCAT,但是這很奇怪,因爲這是來自antlr軟件包。

無論如何,我被卡住了,我很好奇我怎樣才能使用group_concat(或任何其他數據庫特定於gorm的grails功能)。我使用的Grails 2.0.4

回答

0

嘗試這樣:

Yourdomain.withSession{ session -> 
    session.createSQLQuery(yourQueryWithGroup_concat).setLong(yourParameterName,yourParameterValue).list() 
}