Skip to content
Advertisement

Hibernate with Sql Server fail for nvarchar field with “No Dialect mapping…”

I’m using Hibernate’s JPA-Implementation to access our SQL Server 2012 database.

When trying to select a nvarchar field in a native query, I get an exception “No Dialect mapping for JDBC type: -9”.

It looks much like No Dialect mapping for JDBC type: -9 with Hibernate 4 and SQL Server 2012 or No Dialect mapping for JDBC type: -9 but I couldn’t find a solution for me there (both are not using JPA).

My database setup:

JavaScript

My code:

JavaScript

My persistence.xml

JavaScript

With sql logging enable, I get this output in my console

JavaScript

I’m using

  • hibernate-core-4.3.10.Final.jar
  • hibernate-entitymanager-4.3.10.Final.jar
  • hibernate-jpa-2.1-api-1.0.0.Final.jar
  • hibernate-commons-annotations-4.0.5.Final.jar
  • sqljdbc41.jar

What I’ve tried:

  • using a varchar instead of nvarchar makes it work, but I need nvarchar
  • using jpql instead of sql works (see my example code), but I need a native query
  • I tried sqljdbc4.jar in Version 4.0 and 4.1 and I tried sqljdbc41.jar
  • I head about subclassing the SQL Server Dialect class, but did not have any success with that
  • I added <property name="dialect" value="org.hibernate.dialect.SQLServerDialect" /> to my persistence.xml (right behind the password property)
  • I added <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" /> to my persistence.xml
  • I changed the persistence provider to <provider>org.hibernate.ejb.HibernatePersistence</provider>

Advertisement

Answer

I was able to resolve that issue by subclassing the SQLServerDialect:

JavaScript

and referencing it in my persistence.xml:

JavaScript

PS: It seems to be fixed with hibernate 5.1 according to this ticket: https://hibernate.atlassian.net/browse/HHH-10183

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement