英文:
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(
"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()})"
}
)
}
}
答案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(
"myFunction",
new MyFunction(...));
}
private static final class MyFunction implements SQLFunction {
...
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论