registerFunction migration to Hibernate 6

Hello,

Need help migrating to Hibernate 6.x and below is what I have in Hibernate 5.x :

public class CustomOracle10Dialect extends Oracle10gDialect {
    public CustomOracle10Dialect() {
        super();
        // SINGLE_COLUMN_FROM_JSON_TABLE function used to replace large in condition for Oracle
        registerFunction(SelectSingleColumnFromJsonTableOracleFunction.JSON_TABLE_FN_NAME,
                new SelectSingleColumnFromJsonTableOracleFunction());
    }
}

SelectSingleColumnFromJsonTableOracleFunction implements SQLFunction, overrides below -

   @Override
    public Type getReturnType(Type firstArgumentType, Mapping mapping) throws QueryException {
        return new CustomCollectionType(SingleFieldCollectionType.class, "", "");
    }

    @Override
    @SuppressWarnings({"rawtypes", "unchecked"})
    public String render(Type firstArgumentType, List arguments, SessionFactoryImplementor factory) throws QueryException {
        return transformArgsToJsonFnQueryStub((List<Object>)arguments);
    }

transformArgsToJsonFnQueryStub returns a string of StringBuilder.

@beikov Tagging you here as I have seen many of your replies to similar posts.

Hello, to register custom functions you should create custom a org.hibernate.boot.model.FunctionContributor and provide it as a service in your application by creating a file META-INF/services/org.hibernate.boot.model.FunctionContributor that contains its fully qualified class name.

Also, since Hibernate 6 version-specific dialects have been deprecated, so I would suggest using org.hibernate.dialect.OracleDialect.

While I try the suggested, how can I register my custom SelectSingleColumnFromJsonTableOracleFunction with custom return i.e. CustomCollectionType ?

If you want more control on the return type of your custom function you can extend AbstractSqmSelfRenderingFunctionDescriptor, and pass a FunctionReturnTypeResolver (e.g. one of Hibernate’s StandardFunctionReturnTypeResolvers) according to what you need.

Note, however, that org.hibernate.type.CustomCollectionType does not implement ReturnableType, so I doubt that’s the correct type that you want. I suggest looking into what your function actually returns, you probably want to use CustomType instead.

@mbladel Is there an example/sample that you could point me to ?

You can look at any of the inheritor classes in Hibernate itself for examples. Also, you can look at SqmFunctionRegistry descriptor builders, maybe your custom function can be registered using one of those without the need of a custom implementation.