Hibernate 6 QuarkusH2Dialect registerFunction removed

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

Hibernate 6 QuarkusH2Dialect registerFunction removed

问题

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

  1. import io.quarkus.hibernate.orm.runtime.dialect.QuarkusH2Dialect
  2. import org.hibernate.dialect.function.SQLFunction
  3. import org.hibernate.engine.spi.Mapping
  4. import org.hibernate.engine.spi.SessionFactoryImplementor
  5. import org.hibernate.type.StringType
  6. import org.hibernate.type.Type
  7. class MyDialect : QuarkusH2Dialect() {
  8. init {
  9. registerFunction(
  10. "calculate_hash",
  11. object : SQLFunction {
  12. override fun hasArguments() = true
  13. override fun hasParenthesesIfNoArguments() = false
  14. override fun getReturnType(firstArgumentType: Type?, mapping: Mapping?) = StringType.INSTANCE
  15. override fun render(firstArgumentType: Type?, arguments: List<Any?>?, factory: SessionFactoryImplementor?) =
  16. "hash('calculate_hash', ${arguments?.first()})"
  17. }
  18. )
  19. }
  20. }
英文:

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?

  1. import io.quarkus.hibernate.orm.runtime.dialect.QuarkusH2Dialect
  2. import org.hibernate.dialect.function.SQLFunction
  3. import org.hibernate.engine.spi.Mapping
  4. import org.hibernate.engine.spi.SessionFactoryImplementor
  5. import org.hibernate.type.StringType
  6. import org.hibernate.type.Type
  7. class MyDialect : QuarkusH2Dialect() {
  8. init {
  9. registerFunction(
  10. &quot;calculate_hash&quot;,
  11. object : SQLFunction {
  12. override fun hasArguments() = true
  13. override fun hasParenthesesIfNoArguments() = false
  14. override fun getReturnType(firstArgumentType: Type?, mapping: Mapping?) = StringType.INSTANCE
  15. override fun render(firstArgumentType: Type?, arguments: List&lt;Any?&gt;?, factory: SessionFactoryImplementor?) =
  16. &quot;hash(&#39;calculate_hash&#39;, ${arguments?.first()})&quot;
  17. }
  18. )
  19. }
  20. }

答案1

得分: 1

只需扩展 org.hibernate.dialect.H2Dialect

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

  1. quarkus.hibernate-orm.metadata-builder-contributor=com.acme.MyMetadataBuilderConstributor
  1. public class CustomMetadataBuilderContributor implements org.hibernate.boot.spi.MetadataBuilderContributor,
  2. FunctionContributor {
  3. @Override
  4. public void contribute(MetadataBuilder metadataBuilder) {
  5. metadataBuilder.applyFunctions(this);
  6. }
  7. @Override
  8. public void contributeFunctions(FunctionContributions functionContributions) {
  9. TypeConfiguration typeConfiguration = functionContributions.getTypeConfiguration();
  10. functionContributions.getFunctionRegistry().register(
  11. "myFunction",
  12. new MyFunction(...));
  13. }
  14. private static final class MyFunction implements SQLFunction {
  15. ...
  16. }
  17. }

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

英文:

Just extend org.hibernate.dialect.H2Dialect.

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

  1. quarkus.hibernate-orm.metadata-builder-contributor=com.acme.MyMetadataBuilderConstributor
  1. public class CustomMetadataBuilderContributor implements org.hibernate.boot.spi.MetadataBuilderContributor,
  2. FunctionContributor {
  3. @Override
  4. public void contribute(MetadataBuilder metadataBuilder) {
  5. metadataBuilder.applyFunctions(this);
  6. }
  7. @Override
  8. public void contributeFunctions(FunctionContributions functionContributions) {
  9. TypeConfiguration typeConfiguration = functionContributions.getTypeConfiguration();
  10. functionContributions.getFunctionRegistry().register(
  11. &quot;myFunction&quot;,
  12. new MyFunction(...));
  13. }
  14. private static final class MyFunction implements SQLFunction {
  15. ...
  16. }
  17. }

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:

确定