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

后端 未结 5 1618
终归单人心
终归单人心 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: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!

提交回复
热议问题