Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

registerFunction removed from hibernate 6

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)"));
    }
}
like image 235
anil Avatar asked May 28 '26 14:05

anil


1 Answers

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.

like image 132
OrangeDog Avatar answered May 31 '26 06:05

OrangeDog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!