BackEnd/JPA

[JPA] Hibernate6 SQLFunctionTemplate not working anymore

샤아이인 2024. 3. 17.

 

1. 문제의 상황

이번에 SpringBoot 3로 migration 하면서 Hibernate도 6으로 변경하게 되었다.

하지만 더 이상 SQLFunctionTemplate을 사용할 수 없다는 점을 알게 되었다...

나의 프로젝트에는 많지는 않지만, 소량의 CustomFuction들이 존재하고 있던 상황이다...

 

다음은 기존 Hibernate5에서 커스텀하여 사용하고 있던 "match"라는 이름의 함수이다.

의존성 자체를 찾을 수 없는 상황이였으며, Hibernate의 SQLFunctionTemplate 자체가 deprecated 되었다는 점 을 알게 되었다.

 

따라서 이를 변경하기 위해 엄청난 삽질을 했는데....

 

나처럼 삽질하지 않도록 하기 위해.... 기록을 남겨본다.

 

2. 해결 방법

1) 다음과 같이 FunctionContributor를 implements 한 후, contributeFunctions를 오버라이딩 한다.

public class CustomMariaDbFunctionContributor implements FunctionContributor {

    @Override
    public void contributeFunctions(FunctionContributions functionContributions) {
        BasicType<Double> resultType = functionContributions
                .getTypeConfiguration()
                .getBasicTypeRegistry()
                .resolve(StandardBasicTypes.DOUBLE);

        functionContributions.getFunctionRegistry()
                .registerPattern("match", "match(?1) against (?2 in boolean mode)", resultType);
    }
}

 

2) 이후 다음 경로에 FunctionContributor 파일을 추가해 준다.

src/main/resources/META-INF/services/org.hibernate.boot.model.FunctionContributor

 

 

3) 이후, 해당 파일에 직접 구현한 CustomFunctionContributor를 등록한다.

이때, 등록하는 이름은 (패키지명. 컨트리뷰터이름) 형태로 등록해야 한다!

 

이전처럼 Dialect를 변경하거나 상속하는 일을 하지 않아도 된다!!

또한 yml 설정파일에서 기존에 다음과 같이 명시한 부분을 제거해줘야 한다.

 

3. 출처

https://aregall.tech/hibernate-6-custom-functions#heading-the-hibernate-meta-hint-file

 

Hibernate 6 custom functions with Spring Data JPA repositories

Hibernate 6 custom functions: how to register custom SQL functions using Hibernate 6 and calling them in Spring Data JPA repositories.

aregall.tech

 

https://stackoverflow.com/questions/72013981/hibernate-6-0-0-final-custom-dialect-not-working-anymore

 

hibernate 6.0.0.Final custom dialect not working anymore

I'm using PostgreSQL 12 and Hibernate 5.6.8 with a custom dialect like: registerFunction("hstore_find", new SQLFunctionTemplate(StandardBasicTypes.BOOLEAN, "(?1 -> ?2 = ?3)&qu...

stackoverflow.com

 

댓글