JPA SQL Server No Dialect mapping for JDBC type: -9

后端 未结 5 1567
终归单人心
终归单人心 2020-11-27 22:06

Iam writing a native query like

Query query = 
  entityManagerUtil.getEntityManager().createNativeQuery(\"SELECT c.NodeID,c.Code,c.Name FROM COM_Location c\"         


        
相关标签:
5条回答
  • 2020-11-27 22:13

    First you have to define what is your dialect class name that you want to use: in hibernate.cfg.xml add your own class address

    <property name="hibernate.dialect">com.nhl.dao.SQlServerDBDialect</property>
    

    then create new class same below

    package com.nhl.dao;
    import java.sql.Types;
    import org.hibernate.dialect.SQLServerDialect;
    import org.hibernate.type.StandardBasicTypes;
    
    public class SQlServerDBDialect extends SQLServerDialect {
    
        public SQlServerDBDialect() {
            super();
            registerHibernateType(Types.NCHAR, StandardBasicTypes.CHARACTER.getName()); 
            registerHibernateType(Types.NCHAR, 1, StandardBasicTypes.CHARACTER.getName());
            registerHibernateType(Types.NCHAR, 255, StandardBasicTypes.STRING.getName());
            registerHibernateType(Types.NVARCHAR, StandardBasicTypes.STRING.getName());
            registerHibernateType(Types.LONGNVARCHAR, StandardBasicTypes.TEXT.getName());
            registerHibernateType(Types.NCLOB, StandardBasicTypes.CLOB.getName());
    
        }
    }
    
    0 讨论(0)
  • 2020-11-27 22:17

    I also face the same issue, So i do R&D and got solution as Do explicit cast like cast(t2.name as varchar), It works for me.

    cast(t2.name as varchar)
    

    Check this url. http://www.coderanch.com/t/565413/ORM/databases/Dialect-mapping-JDBC-type

    0 讨论(0)
  • 2020-11-27 22:22

    The type -9 is java.sql.Types.NVARCHAR. Looking at the sources of the SQLServerDialect variants on https://github.com/hibernate/hibernate-orm/tree/master/hibernate-core/src/main/java/org/hibernate/dialect there is no mapping for nvarchar columns.

    You might want to try to define your own dialect that also registers various NVARCHAR-like definitions:

    public class SQLServer2008DialectWithNvarchar extends SQLServer2008Dialect {
        public SQLServer2008DialectWithNvarchar () {
            registerColumnType( Types.NCLOB, "nvarchar(MAX)" );
            registerColumnType( Types.LONGNVARCHAR, "nvarchar(MAX)" );
            registerColumnType( Types.NVARCHAR, "nvarchar(MAX)" );
            registerColumnType( Types.NVARCHAR, 4000, "nvarchar($1)" );
        }
    }
    

    I based this on the definition for VARCHAR in the SQLServer2005Dialect. You may need to put this class in the org.hibernate.dialect package (or at least I seem to remember there are issues if you don't).

    NOTE: I haven't actually tested this!

    0 讨论(0)
  • 2020-11-27 22:23

    You have to extend the dialect, and register the appropriate Hibernate types for the N* JDBC types, in the constructor:

    public class MyDialect extends SomeOfTheProvidedDialects {
    
        public MyDialect() {
            registerHibernateType(Types.NCHAR, StandardBasicTypes.CHARACTER.getName());
            registerHibernateType(Types.NCHAR, 1, StandardBasicTypes.CHARACTER.getName());
            registerHibernateType(Types.NCHAR, 255, StandardBasicTypes.STRING.getName());
            registerHibernateType(Types.NVARCHAR, StandardBasicTypes.STRING.getName());
            registerHibernateType(Types.LONGNVARCHAR, StandardBasicTypes.TEXT.getName());
            registerHibernateType(Types.NCLOB, StandardBasicTypes.CLOB.getName());
        }
    }
    

    Tested, works fine.

    They should have added these to the Hibernate source code already (in the org.hibernate.dialect.Dialect class)...

    0 讨论(0)
  • 2020-11-27 22:32

    I had a similar issue, my sql is this

    Query nmspQuery = em.createNativeQuery("select aster from MyTable where my_column = 1001")
    

    The datatype for PRODUCT_NODE_ONIX_TYPE in the Oracle 11g is nvarachar2

    I fixed it by casting the value astr to to_char. This worked fine

    Query nmspQuery = em.createNativeQuery("select TO_CHAR(aster) from MyTable where my_column = 1001")
    
    0 讨论(0)
提交回复
热议问题