mysql – SEPARATOR关键字在Hibernate Formula中无法正常工作

我有以下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
点赞