Hibernate:如何使用CONCAT和GROUP_CONCAT [英] Hibernate: how to use CONCAT and GROUP_CONCAT
问题描述
如何在HQL查询中使用 CONCAT()
和 GROUP_CONCAT()
?
关于 concat
:它的工作方式与MySQL中的方式完全相同(它将字符串,它不是一个集合函数)。
您可以将 group_concat
作为sql函数添加到您的配置中。这样你就可以假设底层数据库知道这个函数,并且你的程序绑定到了MySQL。
导入org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.type.StringType;
// ...
myConf.addSqlFunction(group_concat,new StandardSQLFunction(group_concat,new StringType()));
您还指出该函数的输出是一个字符串。没有这个,当你 group_concat
数字字段时,Hibernate会认为结果也是数字和崩溃。
How can I use CONCAT()
and GROUP_CONCAT()
in HQL queries?
About concat
: it works exactly the same way as it does in MySQL (it concatenates strings, it is not an aggregate function).
You can add group_concat
as an sql function to your configuration. This way you assume that the underlaying DB knows this function, and you tie your program 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 the function is a string. Without this when you group_concat
numeric fields Hibernate will assume the result also to be numeric and crash.
这篇关于Hibernate:如何使用CONCAT和GROUP_CONCAT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!