4

I'm using PostgreSQL 12 and Hibernate 5.6.8 with a custom dialect like:

registerFunction("hstore_find",
    new SQLFunctionTemplate(StandardBasicTypes.BOOLEAN, "(?1 -> ?2 = ?3)"));

When I try to migrate to Hibernate 6.0.0, registerFunction and SQLFunctionTemplate do not exist anymore.

How can I migrate this registerFunction to new method of Hibernate 6?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • How to migrate is explained in the release notes/user guide of Hibernate 6. – M. Deinum Apr 26 '22 at 12:29
  • i already read the migration guide here https://github.com/hibernate/hibernate-orm/blob/main/migration-guide.adoc but nothing about how to migrate old dialect... – Mançaux Pierre-Alexandre Apr 26 '22 at 12:45
  • Have you tried looking at the source of standard dialects how they register functions? – Mark Rotteveel Apr 26 '22 at 12:47
  • According to the documentation the `SQLFunctionTemplate` still exists... I suspect the problem you really have is with the changed type system. – M. Deinum Apr 26 '22 at 12:52
  • @M.Deinum on github i can see : //TODO -- re-implement. In 6.0 there is no longer a SQLFunctionTemplate class, so something change...In release not they explain that they change how to work with dialect but i don't find example...i will try by my own ;) – Mançaux Pierre-Alexandre Apr 26 '22 at 12:57
  • @MarkRotteveel yes i read the Dialect class to see how they register function and type but i don't know how to apply this to my case.. i will try... – Mançaux Pierre-Alexandre Apr 26 '22 at 12:57
  • According to [the javadoc](https://docs.jboss.org/hibernate/orm/6.0/javadocs/org/hibernate/dialect/function/SQLFunctionTemplate.html) there is... That's weird. – M. Deinum Apr 26 '22 at 13:02
  • @M.Deinum as you can see in source code this class not exist in this package: https://github.com/hibernate/hibernate-orm/tree/6.0.0/hibernate-core/src/main/java/org/hibernate/dialect/function – Mançaux Pierre-Alexandre Apr 26 '22 at 13:07
  • @M.Deinum It looks like the javadoc directory wasn't clean, I can also link to other classes that no longer exist or where moved to another location. That class doc is not accessible through navigation/search of the javadoc though. – Mark Rotteveel Apr 26 '22 at 14:12

2 Answers2

2

Following worked for me (Hibernate 6.1.1.Final). However, the function is added via MetadataBuilderContributor (example: https://stackoverflow.com/a/51285944).

This code adds PostgreSQL JSON function @>:

    @Override
    public void contribute(MetadataBuilder metadataBuilder) {
        metadataBuilder.applySqlFunction(
                FIRST_JSON_VALUE_CONTAINS_THE_SECOND_FUNCTION,
                new PatternBasedSqmFunctionDescriptor(
                        new PatternRenderer("(?1 @> ?2)"),
                        null,
                        null,
                        null,
                        null,
                        FunctionKind.NORMAL,
                        null
                )
        );
    }

One might need to adjust parameters of PatternBasedSqmFunctionDescriptor, e.g. FunctionReturnTypeResolver parameter.

Andriy
  • 249
  • 3
  • 8
1

Without having tested it, I think you need to use the following in your custom dialect:

@Override
public void initializeFunctionRegistry(QueryEngine queryEngine) {
    BasicTypeRegistry basicTypeRegistry = queryEngine.getTypeConfiguration().getBasicTypeRegistry();
    SqmFunctionRegistry functionRegistry = queryEngine.getSqmFunctionRegistry();
    functionRegistry.registerPattern(
            "hstore_find", 
            "(?1 -> ?2 = ?3)", 
            basicTypeRegistry.resolve( StandardBasicTypes.BOOLEAN ));
    // ...
}

It is possible basicTypeRegistry.resolve( StandardBasicTypes.BOOLEAN ) can be replaced with just StandardBasicTypes.BOOLEAN.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197