【问题标题】:SpringBoot 3 extend PostgresSQL DialectSpring Boot 3 扩展 PostgresQL 方言
【发布时间】:2022-12-29 19:59:11
【问题描述】:

我们有自定义的 postgres 方言,通过扩展 Postgres 方言和 registerFunctions,但看起来这在 spring boot 3 中不受支持。

在 spring boot 3 中实现这个的替代方法是什么?

public class GlobalPostgresDialect extends PostgreSQL10Dialect {

    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";
    public static final String STRING_AGG_DISTINCT_ORDER_BY = "string_agg_distinct_order_by";
    public static final String ARRAY_AGG = "array_agg";
    public static final String ARRAY_AGG_DISTINCT = "array_agg_distinct";
    public static final String ARRAY_AGG_ORDER_BY = "array_agg_order_by";
    public static final String ARRAY_AGG_DISTINCT_ORDER_BY = "array_agg_distinct_order_by";
    public static final String COUNT_DISTINCT_5_ARGS = "count_distinct_5_args";

    public GlobalPostgresDialect() {
        super();
        registerFunction(STRING_AGG, new SQLFunctionTemplate(StandardBasicTypes.STRING, "string_agg(?1, ?2)"));
        registerFunction(STRING_AGG_ORDER_BY, new SQLFunctionTemplate(StandardBasicTypes.STRING, "string_agg(?1, ?2 order by ?3)"));
        registerFunction(STRING_AGG_DISTINCT, new SQLFunctionTemplate(StandardBasicTypes.STRING, "string_agg(distinct ?1, ?2)"));
        registerFunction(STRING_AGG_DISTINCT_ORDER_BY, new SQLFunctionTemplate(StandardBasicTypes.STRING, "string_agg(distinct ?1, ?2 order by ?3)"));
        registerFunction(ARRAY_AGG, new SQLFunctionTemplate(StandardBasicTypes.STRING, "array_agg(?1)"));
        registerFunction(ARRAY_AGG_DISTINCT, new SQLFunctionTemplate(StandardBasicTypes.STRING, "array_agg(distinct ?1)"));
        registerFunction(ARRAY_AGG_ORDER_BY, new SQLFunctionTemplate(StandardBasicTypes.STRING, "array_agg(?1 order by ?2)"));
        registerFunction(ARRAY_AGG_DISTINCT_ORDER_BY, new SQLFunctionTemplate(StandardBasicTypes.STRING, "array_agg(?1, ?2 order by ?2)"));
        registerFunction(COUNT_DISTINCT_5_ARGS, new SQLFunctionTemplate(LongType.INSTANCE, "count(distinct(?1, ?2, ?3, ?4, ?5))"));
    }
}

【问题讨论】:

    标签: spring-boot hibernate spring-data-jpa spring-data


    【解决方案1】:

    您可以使用implement MetadataBuilderContributor并注册

    文档:https://docs.jboss.org/hibernate/stable/orm/javadocs/org/hibernate/boot/spi/MetadataBuilderContributor.html

    例子:

    public class HibernateMetadataBuilderContributor implements MetadataBuilderContributor {
    
        public static final String I_LIKE_FN = "i_like";
        public static final String PERSON_TYPES_CONTAINS_FN = "person_types_contains";
    
        @Override
        public void contribute(final MetadataBuilder metadataBuilder) {
            metadataBuilder.applySqlFunction(I_LIKE_FN, new SQLFunctionTemplate(BooleanType.INSTANCE, "(?1 ilike ?2)"));
            metadataBuilder.applySqlFunction(PERSON_TYPES_CONTAINS_FN, new SQLFunctionTemplate(BooleanType.INSTANCE, "(?1 && ?2::person_type_enum[])", true));
        }
    
    }
    

    但是我不确定您是否还需要通过属性注册它:

     spring:      
        jpa:
          properties:
            hibernate: your.class.complete.package.HibernateMetadataBuilderContributor 
    

    参考:https://github.com/spring-cloud-portfolio/person-service/blob/d2320f569eebb4c73e10f435dee9ff8618e6bf37/src/main/java/com/doroshenko/serhey/person/repository/core/jpa/HibernateMetadataBuilderContributor.java

    您还可以阅读本文以了解为什么按照您的方式进行操作是个坏主意。 https://vladmihalcea.com/hibernate-sql-function-jpql-criteria-api-query/

    【讨论】:

    • SQLFunctionTemplate 看起来这个类在 Hibernate 6 中不可用
    猜你喜欢
    • 2017-07-30
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 2021-06-16
    • 2021-05-02
    • 2021-10-09
    相关资源
    最近更新 更多