2011-02-10 215 views

回答

9

關於concat:它的工作方式與MySQL中的方式完全相同(它連接字符串,它不是聚合函數)。

您可以將group_concat作爲sql函數添加到您的配置中。這樣你就可以假設底層數據庫知道這個函數,並且你的程序綁定到MySQL 。

import org.hibernate.cfg.Configuration; 
import org.hibernate.dialect.function.StandardSQLFunction; 
import org.hibernate.type.StringType; 

// ... 
myConf.addSqlFunction("group_concat", new StandardSQLFunction("group_concat", new StringType())); 

您還指出該函數的輸出是一個字符串。如果沒有這個,當你使用group_concat數字字段時,Hibernate會認爲結果也是數字和崩潰。

+3

+1提你把自己綁在mysql上。例如,microsoft sql server中沒有group_concat。 hibernate的目標是獨立於SQL供應商。 – Terraego

1

子類的話

registerFunction("group_concat", new StandardSQLFunction("group_concat", Hibernate.STRING)); 

,或者如果您使用createSQLQuery使用SQLFunctionTemplate

1

,使用addScalar到列字符串。

SQLQuery query = sessionObj.createSQLQuery("select group_concat(column1,column2) as mycolumn from some table group by someThing"); 
query.addScalar("mycolumn ", Hibernate.STRING); 
+0

Hibernate.STRING從Hibernate版本3.6.X [這裏是廢棄的文檔](https://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/Hibernate.html)已被棄用。 ,所以你必須使用'StringType.INSTANCE'。 –

相關問題