Migration of dialect to Hibernate 6

I am trying to migrate from Hibernate 5 to 6 and I am a bit stuck with following code:

import org.hibernate.dialect.MySQL57Dialect
import org.hibernate.dialect.function.SQLFunctionTemplate
import org.hibernate.dialect.function.StandardSQLFunction
import org.hibernate.type.StandardBasicTypes
import org.hibernate.type.StringType
import java.sql.Types

class CustomMySQLDialect : MySQLDialect(DatabaseVersion.make(5, 70)) {
    init {
        registerFunction("group_concat", StandardSQLFunction("group_concat", StringType()))
        registerFunction("match_against", SQLFunction(StandardBasicTypes.DOUBLE, "match (?1) against (?2 in boolean mode)"))

        registerHibernateType(Types.FLOAT, StandardBasicTypes.DOUBLE.name)
        registerHibernateType(Types.BIGINT, StandardBasicTypes.LONG.name)
        registerHibernateType(Types.TINYINT, StandardBasicTypes.LONG.name)
        registerHibernateType(Types.INTEGER, StandardBasicTypes.LONG.name)
        registerHibernateType(Types.DECIMAL, StandardBasicTypes.LONG.name)
    }
}

It seems that the registerFunction and registerHibernateType has been removed and their replacement is not trivial. Is there a way to od this simply?

There is no easy way, but I think you don’t need registerHibernateType anymore. As for registerFunction, I’d suggest you rather introduce a org.hibernate.boot.model.FunctionContributor:

public class MyFunctionContributor imlements org.hibernate.boot.model.FunctionContributor {
    public void contributeFunctions(FunctionContributions functionContributions) {
        functionContributions.getFunctionRegistry().registerAlternateKey("group_concat", "listagg");
        functionContributions.getFunctionRegistry().registerPattern("match_against", "match (?1) against (?2 in boolean mode)", functionContributions.getTypeConfiguration().getBasicTypeRegistry().resolve(StandardBasicTypes.DOUBLE));
    }
}

Note that you also need a file META-INF/services/org.hibernate.boot.model.FunctionContributor that contains the fully qualified class name of MyFunctionContributor.

But I need this :slight_smile: I want to avoid fetching numbers from database as Short or Int and I want to always get Long.

If you want to affect how native query results are materialized, you have to override the Dialect#resolveSqlTypeDescriptor method similar to this:

public class MyDialect extends MySQLDialect {

//...

	public JdbcType resolveSqlTypeDescriptor(
			String columnTypeName,
			int jdbcTypeCode,
			int precision,
			int scale,
			JdbcTypeRegistry jdbcTypeRegistry) {
		
		switch ( jdbcTypeCode ) {
			case Types.FLOAT:
				jdbcTypeCode = Types.DOUBLE;
				break;
			case Types.TINYINT:
			case Types.INTEGER:
			case Types.DECIMAL:
				jdbcTypeCode = Types.BIGINT;
				break;
		}
		return super.resolveSqlTypeDescriptor( columnTypeName, jdbcTypeCode, precision, scale, jdbcTypeRegistry );
	}
}

This seems to help, thanks!