hibernate No Dialect mapping for JDBC type 1111 problem solution

Posted by strangesoul on Thu, 10 Mar 2022 04:23:36 +0100

No Dialect mapping for JDBC type: 1111 problem solution

Error reporting reason

Because each database has its own dialect, and the simple understanding of dialect is to make a mapping relationship for the unique data types and functions of some databases, and 1111 can't find the corresponding value of dialect because there is no mapping relationship

package org.hibernate.dialect;

public final class TypeNames {
	/**
	 * get default type name for specified type
	 *
	 * @param typeCode the type key
	 *
	 * @return the default type name associated with specified key
	 *
	 * @throws MappingException Indicates that no registrations were made for that typeCode
	 */
	public String get(final int typeCode) throws MappingException {
		final Integer integer = Integer.valueOf( typeCode );
		final String result = defaults.get( integer );
		if ( result == null ) {
			throw new MappingException( "No Dialect mapping for JDBC type: " + typeCode );
		}
		return result;
	}
    
    ...
}

From the above code, when typeCode is 1111, there is no value with key 1111 in defaults and there is no mapping, so an exception is thrown

package org.hibernate.dialect;

public abstract class Dialect implements ConversionContext {
    protected Dialect() {
        // standard sql92 functions (can be overridden by subclasses)
		registerFunction( "substring", new SQLFunctionTemplate( StandardBasicTypes.STRING, "substring(?1, ?2, ?3)" ) );
		registerFunction( "locate", new SQLFunctionTemplate( StandardBasicTypes.INTEGER, "locate(?1, ?2, ?3)" ) );
		registerFunction( "trim", new SQLFunctionTemplate( StandardBasicTypes.STRING, "trim(?1 ?2 ?3 ?4)" ) );
        ...
        //map second/minute/hour/day/month/year to ANSI extract(), override on subclasses
		registerFunction( "second", new SQLFunctionTemplate(StandardBasicTypes.INTEGER, "extract(second from ?1)") );
		registerFunction( "minute", new SQLFunctionTemplate(StandardBasicTypes.INTEGER, "extract(minute from ?1)") );
		registerFunction( "hour", new SQLFunctionTemplate(StandardBasicTypes.INTEGER, "extract(hour from ?1)") );
        ...
        registerColumnType( Types.BIT, "bit" );
		registerColumnType( Types.BOOLEAN, "boolean" );
		registerColumnType( Types.TINYINT, "tinyint" );
        ...
        // register hibernate types for default use in scalar sqlquery type auto detection
		registerHibernateType( Types.BIGINT, StandardBasicTypes.BIG_INTEGER.getName() );
		registerHibernateType( Types.BINARY, StandardBasicTypes.BINARY.getName() );
		registerHibernateType( Types.BIT, StandardBasicTypes.BOOLEAN.getName() );
        ...
    }
    
    ...
}

The above code is the default mapping in the hibernate dialect class. Each database has different mappings, so the dialect packages of different databases inherit from this class and carry out some unique mapping relationships

package java.sql;

public class Types {
    
        /**
         * <P>The constant in the Java programming language, sometimes referred
         * to as a type code, that identifies the generic SQL type
         * <code>BIT</code>.
         */
        public final static int BIT             =  -7;

        /**
         * <P>The constant in the Java programming language, sometimes referred
         * to as a type code, that identifies the generic SQL type
         * <code>TINYINT</code>.
         */
        public final static int TINYINT         =  -6;

        /**
         * <P>The constant in the Java programming language, sometimes referred
         * to as a type code, that identifies the generic SQL type
         * <code>SMALLINT</code>.
         */
        public final static int SMALLINT        =   5;

        /**
         * <P>The constant in the Java programming language, sometimes referred
         * to as a type code, that identifies the generic SQL type
         * <code>INTEGER</code>.
         */
        public final static int INTEGER         =   4;

        /**
         * <P>The constant in the Java programming language, sometimes referred
         * to as a type code, that identifies the generic SQL type
         * <code>BIGINT</code>.
         */
        public final static int BIGINT          =  -5;
    
        /**
         * The constant in the Java programming language that indicates
         * that the SQL type is database-specific and
         * gets mapped to a Java object that can be accessed via
         * the methods <code>getObject</code> and <code>setObject</code>.
         */
        public final static int OTHER           = 1111;
    
    ...
}

Solution

From the above code, we can see that 1111 is other, unlike the type that will be mapped

So we manually map the relationship, and we don't know what 1111 represents. I find the corresponding table through the sql that reports an error, check the strange types in the table structure, and then check whether there is a corresponding relationship. If not, I will manually add the mapping relationship

The problem in my project is that the field type of a table in kingbase database is serial, and there is no mapping relationship in the dialect, so I manually added the mapping

public class Kingbase8Dialect extends org.hibernate.dialect.Kingbase8Dialect {

    public Kingbase8Dialect() {
        // Add the mapping of other, i.e. 1111
        this.registerHibernateType(Types.OTHER, StandardBasicTypes.BIG_INTEGER.getName());
        // Some other mapping relationships
        /*registerFunction( "substring", new SQLFunctionTemplate( StandardBasicTypes.STRING, "substring(?1, ?2, ?3)" ) );
        registerColumnType( Types.BIT, "bit" );*/
    }
}

After setting the dialect, you need to configure the dialect in hibernate

spring:
  jpa:
    hibernate:
      ddl-auto: none
    properties:
      hibernate:
      	#dialect: org.hibernate.dialect.Kingbase8Dialect
   		#Modify to our own dialect class
        dialect: com.xxx.xxx.Kingbase8Dialect
        

So far, the problem has been solved

summary

Some problems, research, look at the source code, you can understand the causes of the problems, so as to solve the problems. From our current problem,

What I encountered and solved is only a problem of No Dialect mapping for JDBC type: 1111, but by looking at the source code, we can learn a lot of derivative problems

For example, if there is a problem of No Dialect mapping for xxx in the future, we will immediately know where the problem is.

This is my resolution and solution to the problem of No Dialect mapping for JDBC type: 1111. I hope it will be helpful to you who are watching

Topics: Java Database Hibernate