registerFunction removed from hibernate 6

725 views Asked by At

We are Migrating from springboot 2 to 3 which includes hibernate 5 to 6 migration

public class ExtendSqlServer2012Dialect extends SqlServer2012Dialect{
    public ExtendSqlServer2012Dialect(){
        registerFunction("datediffhr", new SQLFunctionemplate(StandardBasicTypes.INTEGER,"datediff(hour,?1,?2)"));
        registerFunction("dateaddhr", new SQLFunctionemplate(StandardBasicTypes.TIMESTAMP,"dateaddhr(hour,0,?1)"));
    }
}
1

There are 1 answers

0
OrangeDog On

Dialect now has a method initializeFunctionRegistry. The direct equivalent would be:

public class ExtendSqlServer2012Dialect extends SqlServerDialect{
    @Override
    public void initializeFunctionRegistry(FunctionContributions functionContributions) {
        super.initializeFunctionRegistry(functionContributions);
        SqmFunctionRegistry registry = functionContributions.getFunctionRegistry();
        TypeConfiguration types = functionContributions.getTypeConfiguration();

        new PatternFunctionDescriptorBuilder(registry, "datediffhr", FunctionKind.NORMAL, "datediff(hour,?1,?2)")
                .setExactArgumentCount(2)
                .setInvariantType(types.getBasicTypeForJavaType(integer.class))
                .register();
    }
}

You may find registry.registerNamed easier to use, or you may like to use a different SqmFunctionDescriptor subclass.

You can also use an independent FunctionContributor registered via SPI instead of extending the Dialect.