Hibernate 6 SQLFunctionTemplate 替代方案

Jan*_*ana 6 hibernate hibernate-criteria spring-data spring-data-jpa spring-boot

我们有 HibernateMetadataBuilderContributor 如下所示。它适用于 Hibernate 5 或 Spring boot 2.7。但当我们迁移到 Hibernate 6 或 Spring boot 3 时就不起作用了。

public class HibernateMetadataBuilderContributor implements MetadataBuilderContributor {

    public static final String STRING_AGG = "string_agg";
    public static final String STRING_AGG_ORDER_BY = "string_agg_order_by";
    public static final String STRING_AGG_DISTINCT = "string_agg_distinct";

    @Override
    public void contribute(final MetadataBuilder metadataBuilder) {
        metadataBuilder.applySqlFunction(STRING_AGG, new SQLFunctionTemplate(StandardBasicTypes.STRING, "string_agg(?1, ?2)"));
        metadataBuilder.applySqlFunction(STRING_AGG_ORDER_BY, new SQLFunctionTemplate(StandardBasicTypes.STRING, "string_agg(?1, ?2 order by ?3)"));
        metadataBuilder.applySqlFunction(STRING_AGG_DISTINCT, new SQLFunctionTemplate(StandardBasicTypes.STRING, "string_agg(distinct ?1, ?2)"));

    }
}
Run Code Online (Sandbox Code Playgroud)

在 Hibernate 6 中找不到 SQLFunctionTemplate,有什么替代方案吗?

Gav*_*ing 0

第一:在H6中你不需要这样做!

HQL 现在带有一个内置listagg()函数,它会自动转换为string_agg()那些数据库上的名称。请查看有关 HQL 的用户指南章节,了解内置可移植函数的完整列表。

https://docs.jboss.org/hibernate/orm/6.2/userguide/html_single/Hibernate_User_Guide.html#hql-aggregate-functions-orderedset

现在,话虽如此……

SQLFunctionH6 中的等效接口是SqmFunctionDescriptor,它有许多内置实现,您可以查看以获取灵感,包括PatternBasedSqmFunctionDescriptor.

但我们提供的基本的、简化的、“用户友好的”实现仍然是org.hibernate.dialect.function.StandardSQLFunction,它现在已经适应了新的、更强大的SqmFunctionDescriptor框架。