Custom function in Hibernate 6.x from 5.3

Hello,
I’d like to ask a question regarding moving from hibernate-core 5.3.6 (hibernate-entitymanager,hibernate-java8) to the new hibernate-core 6.x.

I have old custom functions which use old registerFunction calls and are extending from PostgreSQL9Dialect/H2Dialect.
I replaced the code to this by overriding initializeFunctionRegistry(FunctionContributions functionContributions) and registering my functions there.

But I have still problem with the code extending StandardSQLFunction. It goes like this:

package sk.mindit.commons.persistence;

import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.type.Type;

import java.util.List;
import java.util.stream.Collectors;

public class AclComputeFunction extends StandardSQLFunction {
    public AclComputeFunction(String name) {
        super(name);
    }

    public AclComputeFunction(String name, Type registeredType) {
        super(name, registeredType);
    }

    @Override
    public String render(Type firstArgumentType, List arguments, SessionFactoryImplementor sessionFactory) {
        return super.render(firstArgumentType, (List) arguments.stream()
                .map(s -> "(" + s + ")::bit(12)")
                .collect(Collectors.toList()), sessionFactory);
    }
}

I’d really like to have a simple way how to rewrite this code, I know that know there’s this function:

render(SqlAppender sqlAppender, List<? extends SqlAstNode> sqlAstArguments, SqlAstTranslator<?> translator)

But overriding it seems like an overkill. Is there a simple (safe) way how to write this, or could you please point me to some examples ?

Thanks.

It’s not an overkill, you just need to call append on the SqlAppender e.g.

public void render(SqlAppender sqlAppender, List<? extends SqlAstNode> sqlAstArguments, SqlAstTranslator<?> translator) {
    sqlAppender.append('(');
    sqlAstArguments.get(0).accept(translator);
    sqlAppender.append(")::bit(12)");
}

Hm, ok I’ll try it. I thought it’d be harder :slight_smile: