Hibernate:如何使用CONCAT和GROUP_CONCAT

mrc*_*ool 11 java mysql hibernate hql

如何使用CONCAT()GROUP_CONCAT()在HQL查询?

vbe*_*nce 10

关于concat:它与MySQL中的工作方式完全相同(它连接字符串,它不是聚合函数).

您可以将group_concatsql函数添加到配置中.这样你就可以假设底层数据库知道这个函数,并且你将程序绑定到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()));
Run Code Online (Sandbox Code Playgroud)

您还指出函数的输出是一个字符串.如果你没有这个group_concat数字字段,Hibernate将假设结果也是数字和崩溃.

  • +1提及你绑自己的mysql.例如,microsoft sql server中没有group_concat.hibernate的目标是独立于sql供应商. (3认同)