我该怎么用 CONCAT()
和 GROUP_CONCAT()
在HQL查询?
我该怎么用 CONCAT()
和 GROUP_CONCAT()
在HQL查询?
关于 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将假设结果也是数字和崩溃。
关于 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将假设结果也是数字和崩溃。
方言的子类
和
registerFunction("group_concat", new StandardSQLFunction("group_concat", Hibernate.STRING));
如果您使用的是createSQLQuery,请将该列的addScalar用作String。
SQLQuery query = sessionObj.createSQLQuery("select group_concat(column1,column2) as mycolumn from some table group by someThing");
query.addScalar("mycolumn ", Hibernate.STRING);