Hibernate 6 QuarkusH2Dialect registerFunction removed

huangapple go评论77阅读模式
英文:

Hibernate 6 QuarkusH2Dialect registerFunction removed

问题

我正在使用Quarkus和Hibernate。有一个自定义类,它扩展了QuarkusH2Dialect。但在迁移到Hibernate 6之后,它不再起作用了。它已被移除。您能提供一些建议,如何修复它吗?

import io.quarkus.hibernate.orm.runtime.dialect.QuarkusH2Dialect
import org.hibernate.dialect.function.SQLFunction
import org.hibernate.engine.spi.Mapping
import org.hibernate.engine.spi.SessionFactoryImplementor
import org.hibernate.type.StringType
import org.hibernate.type.Type

class MyDialect : QuarkusH2Dialect() {

    init {
        registerFunction(
            "calculate_hash",
            object : SQLFunction {
                override fun hasArguments() = true
                override fun hasParenthesesIfNoArguments() = false
                override fun getReturnType(firstArgumentType: Type?, mapping: Mapping?) = StringType.INSTANCE
                override fun render(firstArgumentType: Type?, arguments: List<Any?>?, factory: SessionFactoryImplementor?) =
                    "hash('calculate_hash', ${arguments?.first()})"
            }
        )
    }
}
英文:

I’m using Quarkus and Hibernate. There is a custom class which extends QuarkusH2Dialect. But after migration to Hibernate 6 it doesn’t work anymore. It's been removed. Could you please provide some pieces of advice, how to fix it?

import io.quarkus.hibernate.orm.runtime.dialect.QuarkusH2Dialect
import org.hibernate.dialect.function.SQLFunction
import org.hibernate.engine.spi.Mapping
import org.hibernate.engine.spi.SessionFactoryImplementor
import org.hibernate.type.StringType
import org.hibernate.type.Type

class MyDialect : QuarkusH2Dialect() {

    init {
        registerFunction(
            &quot;calculate_hash&quot;,
            object : SQLFunction {
                override fun hasArguments() = true
                override fun hasParenthesesIfNoArguments() = false
                override fun getReturnType(firstArgumentType: Type?, mapping: Mapping?) = StringType.INSTANCE
                override fun render(firstArgumentType: Type?, arguments: List&lt;Any?&gt;?, factory: SessionFactoryImplementor?) =
                    &quot;hash(&#39;calculate_hash&#39;, ${arguments?.first()})&quot;
            }
        )
    }
}

答案1

得分: 1

只需扩展 org.hibernate.dialect.H2Dialect

或者更好的方法是通过自定义的 MetadataBuilderContributor 注册您的函数:

quarkus.hibernate-orm.metadata-builder-contributor=com.acme.MyMetadataBuilderConstributor
public class CustomMetadataBuilderContributor implements org.hibernate.boot.spi.MetadataBuilderContributor,
        FunctionContributor {

    @Override
    public void contribute(MetadataBuilder metadataBuilder) {
        metadataBuilder.applyFunctions(this);
    }

    @Override
    public void contributeFunctions(FunctionContributions functionContributions) {
        TypeConfiguration typeConfiguration = functionContributions.getTypeConfiguration();
        functionContributions.getFunctionRegistry().register(
                "myFunction",
                new MyFunction(...));
    }

    private static final class MyFunction implements SQLFunction {
        ...
    }
}

相关链接: https://github.com/quarkusio/quarkus/issues/32936

英文:

Just extend org.hibernate.dialect.H2Dialect.

Or, better, register your function through a custom MetadataBuilderContributor:

quarkus.hibernate-orm.metadata-builder-contributor=com.acme.MyMetadataBuilderConstributor
public class CustomMetadataBuilderContributor implements  org.hibernate.boot.spi.MetadataBuilderContributor,
        FunctionContributor {

    @Override
    public void contribute(MetadataBuilder metadataBuilder) {
        metadataBuilder.applyFunctions(this);
    }

    @Override
    public void contributeFunctions(FunctionContributions functionContributions) {
        TypeConfiguration typeConfiguration = functionContributions.getTypeConfiguration();
        functionContributions.getFunctionRegistry().register(
                &quot;myFunction&quot;,
                new MyFunction(...));
    }

    private static final class MyFunction implements SQLFunction {
        ...
    }
}

Related: https://github.com/quarkusio/quarkus/issues/32936

huangapple
  • 本文由 发表于 2023年7月7日 02:40:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定