How to register non-standarized SQL functions manually in Spring Boot application?

前端 未结 3 1503
情深已故
情深已故 2021-01-16 04:49

I\'m using JPA query in my current spring-boot project. How can I add non-standardized SQL functions like GROUP_CONCAT?

Prior, to my previo

3条回答
  •  难免孤独
    2021-01-16 05:08

    In case someone is having issues when registering this in a SpringBoot app this is the right way:

    Create a class that implements: MetadataBuilderContributor interface.

    package com.application.config;
    
    public class SqlFunctionsMetadataBuilderContributor implements MetadataBuilderContributor {
    
      @Override
      public void contribute(MetadataBuilder metadataBuilder) {
        metadataBuilder.applySqlFunction(
            "STRING_AGG",
            new StandardSQLFunction(
                "STRING_AGG",
                StandardBasicTypes.STRING
            )
        );
      }
    }
    

    In your application .yml (or .properties) refer to the previously created class in the following properties path: spring.jpa.properties.hibernate.metadata_builder_contributor

      spring:      
        jpa:
          properties:
            hibernate:
              metadata_builder_contributor: com.application.config.SqlFunctionsMetadataBuilderContributor
    

提交回复
热议问题