Home > other >  Migrate registerFunction and registerHibernateType to Hibernate6
Migrate registerFunction and registerHibernateType to Hibernate6

Time:11-29

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?

EDIT: I added imports for clarification.

CodePudding user response:

The second part can be handled as follows:

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 );
    }
}
  • Related