Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can I use CONCAT() and GROUP_CONCAT() in HQL queries?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
552 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...