我有以下 Hibernate forumla 查询,我可以在 mysql workbanch 中执行。
select group_concat(distinct t.column_1_name SEPARATOR ', ') from table_name t and t.fk_record_id = record_id
在使用 Hibernate 执行此查询时,hibernate 将父表附加到 SEPRATOR 关键字,如下面的查询所示。
select group_concat(distinct t.column_1_name parent_table.SEPARATOR ', ') from table_name t and t.fk_record_id = record_id
这里 hibernate 没有将 SEPRATOR 视为关键字。有人对此有任何想法吗?
最佳答案
您可以添加 SEPARATOR
作为关键字。实现您自己的 DialectResolver
并将关键字小写 添加到生成的方言中:
public class MyDialectResolver implements DialectResolver {
public Dialect resolveDialect(DialectResolutionInfo info) {
for (Database database : Database.values()) {
Dialect dialect = database.resolveDialect(info);
if (dialect != null) {
dialect.getKeywords().add("separator");
return dialect;
}
}
return null;
}
}
对于 5.2.13/5.3.0 之前的 Hibernate 版本也是如此:
public class MyDialectResolver extends StandardDialectResolver {
protected Dialect resolveDialectInternal(DatabaseMetaData metaData) throws SQLException {
Dialect dialect = super.resolveDialectInternal(metaData);
dialect.getKeywords().add("separator");
return dialect;
}
}
然后您必须告诉 Hibernate 使用您的方言解析器。例如,在 JPA 中,您可以在 persistence.xml 中执行此操作:
<persistence>
<persistence-unit>
...
<property name="hibernate.dialect_resolvers" value="mypackage.MyDialectResolver"/>
</persistence-unit>
</persistence>
这同样适用于其他方言中的聚合函数。例如在 Oracle 中缺少 WITHIN
关键字。
还有另一种选择,它更独立于数据库(我更喜欢这种选择)。创建以下 SQLFunction
:
public class ListAggFunction implements SQLFunction {
/**
* The pattern that describes how the function is build in SQL.
*
* Replacements:
* {path} - is replaced with the path of the list attribute
* {separator} - is replaced with the separator (defaults to '')
* {orderByPath} - is replaced by the path that is used for ordering the elements of the list
*/
private String pattern;
/**
* Creates a new ListAggFunction definition which uses the ANSI SQL:2016 syntax.
*/
public ListAggFunction() {
this("LISTAGG(DISTINCT {path}, {separator}) WITHIN GROUP(ORDER BY {orderByPath})");
}
/**
* Creates a new ListAggFunction definition which uses a database specific syntax.
*
* @param pattern The pattern that describes how the function is build in SQL.
*/
public ListAggFunction(String pattern) {
this.pattern = pattern;
}
public Type getReturnType(Type firstArgumentType, Mapping mapping) throws QueryException {
return StringType.INSTANCE;
}
public boolean hasArguments() {
return true;
}
public boolean hasParenthesesIfNoArguments() {
return true;
}
public String render(Type firstArgumentType, List arguments,
SessionFactoryImplementor factory) throws QueryException {
if (arguments.isEmpty() || arguments.size() > 3) {
throw new IllegalArgumentException(
"Expected arguments for 'listagg': path [, separator [, order by path]]");
}
String path = (String) arguments.get(0);
String separator = arguments.size() < 2 ? "''" : (String) arguments.get(1);
String orderByPath = arguments.size() <= 2 ? path : (String) arguments.get(2);
return StringUtils.replaceEach(this.pattern, new String[] { "{path}", "{separator}", "{orderByPath}" },
new String[] { path, separator, orderByPath });
}
}
你可以像上面的关键字一样在DialectResolver中注册这个函数:
if ("MySQL".equals(info.getDatabaseName()) || "H2".equals(info.getDatabaseName())) {
dialect.getFunctions().put("listagg", new ListAggFunction("GROUP_CONCAT(DISTINCT {path} ORDER BY {orderByPath} SEPARATOR {separator})"));
} else {
dialect.getFunctions().put("listagg", new ListAggFunction());
}
现在您可以在 JPQL/HQL/Criteria 查询中使用此函数,而无需考虑方言的语法:
SELECT e.group, listagg(e.stringProperty, ', ') FROM Entity e GROUP BY e.group
关于mysql - SEPARATOR 关键字在 Hibernate 公式中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32688660/