[ACCEPTED]-Hibernate: how to use CONCAT and GROUP_CONCAT-hql
Accepted answer
About concat
: it works exactly the same way as 9 it does in MySQL (it concatenates strings, it 8 is not an aggregate function).
You can add 7 group_concat
as an sql function to your configuration. This 6 way you assume that the underlaying DB knows 5 this function, and you tie your program 4 to 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()));
You also indicate that the output of 3 the function is a string. Without this when 2 you group_concat
numeric fields Hibernate will assume 1 the result also to be numeric and crash.
subclass the dialect
and
registerFunction("group_concat", new StandardSQLFunction("group_concat", Hibernate.STRING));
or use SQLFunctionTemplate
0
If you are using createSQLQuery, use addScalar 1 to that column as String.
SQLQuery query = sessionObj.createSQLQuery("select group_concat(column1,column2) as mycolumn from some table group by someThing");
query.addScalar("mycolumn ", Hibernate.STRING);
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.