![[JPA] Hibernate6 SQLFunctionTemplate not working anymore [JPA] Hibernate6 SQLFunctionTemplate not working anymore](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
1. 문제의 상황
이번에 SpringBoot 3로 migration 하면서 Hibernate도 6으로 변경하게 되었다.
하지만 더 이상 SQLFunctionTemplate을 사용할 수 없다는 점을 알게 되었다...
나의 프로젝트에는 많지는 않지만, 소량의 CustomFuction들이 존재하고 있던 상황이다...
다음은 기존 Hibernate5에서 커스텀하여 사용하고 있던 "match"라는 이름의 함수이다.
![[JPA] Hibernate6 SQLFunctionTemplate not working anymore - 1. 문제의 상황 [JPA] Hibernate6 SQLFunctionTemplate not working anymore - 1. 문제의 상황](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
의존성 자체를 찾을 수 없는 상황이였으며, 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
![[JPA] Hibernate6 SQLFunctionTemplate not working anymore - 2. 해결 방법 [JPA] Hibernate6 SQLFunctionTemplate not working anymore - 2. 해결 방법](https://blog.kakaocdn.net/dn/pW7cN/btsFQwSmQ8t/KjBBiqYV815ZsRDe8gK7kK/img.png)
3) 이후, 해당 파일에 직접 구현한 CustomFunctionContributor를 등록한다.
이때, 등록하는 이름은 (패키지명. 컨트리뷰터이름) 형태로 등록해야 한다!
![[JPA] Hibernate6 SQLFunctionTemplate not working anymore - 2. 해결 방법 [JPA] Hibernate6 SQLFunctionTemplate not working anymore - 2. 해결 방법](https://blog.kakaocdn.net/dn/chiXL1/btsFR2v3hkP/ohOpHZEQ56ZK2JqPH3W490/img.png)
이전처럼 Dialect를 변경하거나 상속하는 일을 하지 않아도 된다!!
또한 yml 설정파일에서 기존에 다음과 같이 명시한 부분을 제거해줘야 한다.
![[JPA] Hibernate6 SQLFunctionTemplate not working anymore - 2. 해결 방법 [JPA] Hibernate6 SQLFunctionTemplate not working anymore - 2. 해결 방법](https://blog.kakaocdn.net/dn/VrxhA/btsFQt89v8S/OMVoK3GYAU9CfB8VLDDYT1/img.png)
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
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
'BackEnd > JPA' 카테고리의 다른 글
[JPA] Unable to find column with logical name: child_name in org.hibernate.mapping.Table and its related supertables and secondary tables (0) | 2023.02.16 |
---|---|
[JPA] QueryDsl에서 Groupy By적용 후 가장 큰 원소 가져오기 (0) | 2022.11.23 |
[JPA] QueryDSL 에서 Select필드로 상속한 Entity 사용시 경험한 문제 (0) | 2022.11.10 |
[JPA] Soft Delete 자동 처리하기 (0) | 2022.11.09 |
[JPA] Open Session In View 더 깊게 (0) | 2022.09.11 |
댓글