Getting "org.hibernate.MappingException: No Dialect mapping for JDBC type: -9" when using Hibernate/JPA in JBoss EAP 6.x/7.x

Solution Unverified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 6.x
    • 7.x
  • Hibernate/JPA
  • Oracle Database
  • Microsoft SQL Server

Issue

I got the ERROR message caused by the foolowing exception "org.hibernate.MappingException: No Dialect mapping for JDBC type: -9" when I use native query in my Hibernate/JPA application:

Caused by: org.hibernate.MappingException: No Dialect mapping for JDBC type: -9
	at org.hibernate.dialect.TypeNames.get(TypeNames.java:76) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.dialect.TypeNames.get(TypeNames.java:99) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.dialect.Dialect.getHibernateTypeName(Dialect.java:592) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.loader.custom.CustomLoader$Metadata.getHibernateType(CustomLoader.java:720) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.performDiscovery(CustomLoader.java:602) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:618) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.loader.Loader.getResultSet(Loader.java:2070) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1859) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.loader.Loader.doQuery(Loader.java:906) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:348) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.loader.Loader.doList(Loader.java:2550) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.loader.Loader.doList(Loader.java:2536) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2366) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.loader.Loader.list(Loader.java:2361) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:340) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1752) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:232) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:157) [hibernate-core-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:268) [hibernate-entitymanager-4.2.17.SP1-redhat-1.jar:4.2.17.SP1-redhat-1]
	... 259 more

Resolution

This is a bug reported in the upstream JIRA Content from hibernate.atlassian.net is not included.HHH-10183. The following JIRAs and bugzilla are raised to backport the fix to EAP 6 and EAP 7. This will be fixed in future releases:

Until the fix is incorporated in EAP 6.x/7.x, please create a custom dialect and specify it in your persistence.xml as a workaround. For example:

  1. Crate a custom dialect:

        package com.example.hibernate.custom.dialect;
    
        import org.hibernate.dialect.Oracle10gDialect;
    
        public class MyCustomDialect extends Oracle10gDialect {
            public MyCustomDialect() {
                super();
                registerHibernateType(Types.NVARCHAR, StringType.INSTANCE.getName());
            }
        }
    

    Note: if you are using other dialect (for example, org.hibernate.dialect.SQLServer2008Dialect), you should extends the class instead of extending Oracle10gDialect.

  2. Specify the custom dialect in your META-INF/persistence.xml:

    <persistence ...>
       <persistence-unit name="...">
          :
          <properties>
             <!-- Properties for Hibernate -->
             <property name="hibernate.dialect" value="com.example.hibernate.custom.dialect.MyCustomDialect"/>
             :
          </properties>
       </persistence-unit>
    </persistence>
    

Root Cause

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.