-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate 3.6 CR2 & MSSQL - JDBC Dialect Mapping Type -9
PostPosted: Tue Oct 19, 2010 9:50 am 
Newbie

Joined: Tue Oct 19, 2010 9:18 am
Posts: 7
Hello!

i'm using JBoss 4.2.2 and try to move to a new Hibernate version. So i copied all necessary libs to jboss, changed some parts of the code and tested my program with the following result:
Code:
javax.persistence.PersistenceException: org.hibernate.MappingException: No Dialect mapping for JDBC type: -9
   at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:629)
   at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:99)
   at de.xxx.sql.DaoServiceImpl.findOne(DaoServiceImpl.java:3371)
   at de.xxx.sql.DaoServiceImpl.findGasVnbId(DaoServiceImpl.java:1819)
    ...
Caused by: org.hibernate.MappingException: No Dialect mapping for JDBC type: -9
   at org.hibernate.dialect.TypeNames.get(TypeNames.java:56)
   at org.hibernate.dialect.TypeNames.get(TypeNames.java:81)
   at org.hibernate.dialect.Dialect.getHibernateTypeName(Dialect.java:370)
   at org.hibernate.loader.custom.CustomLoader$Metadata.getHibernateType(CustomLoader.java:559)
   at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.performDiscovery(CustomLoader.java:485)
   at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:501)
   at org.hibernate.loader.Loader.getResultSet(Loader.java:1796)
   at org.hibernate.loader.Loader.doQuery(Loader.java:674)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
   at org.hibernate.loader.Loader.doList(Loader.java:2220)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
   at org.hibernate.loader.Loader.list(Loader.java:2099)
   at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
   at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
   at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
   at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
   at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:80)
   ... 23 more


After some investigations i found out, that the old jdbc driver does not know the type nvarchar, but the newer driver does (latest sqljdbc 3.0).
I decided to write a own MSSQL-Dialect to map this type -9, here it is:
Code:
package de.xxx.sql;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.sql.Types;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;

public class MSSqlServerDialect extends org.hibernate.dialect.SQLServerDialect {
   private static final Log LOG = LogFactory.getLog(MSSqlServerDialect.class);
   
   private static final String TYPE_STRING=String.class.getCanonicalName();
   private static final String TYPE_BIGDECIMAL=BigDecimal.class.getCanonicalName();
   private static final String TYPE_TIMESTAMP=Timestamp.class.getCanonicalName();
   
   public MSSqlServerDialect() {
      super();
      registerColumnType(Types.NVARCHAR, 255, "nvarchar($l)");
      registerHibernateType(Types.NVARCHAR, Hibernate.STRING.getName());

      registerColumnType(Types.NCHAR, "nchar(1)");
      registerHibernateType(Types.NCHAR, Hibernate.CHARACTER.getName());
       
      registerColumnType(Types.NCLOB, "nvarchar(max)");
      registerHibernateType(Types.NCLOB, Hibernate.CLOB.getName());
   }

   @Override
   public String getTypeName(int code) {
      if (LOG.isDebugEnabled()) {
         LOG.debug("getTypeName() for code \""+code+"\"");
      }
      switch (code) {
         case Types.NVARCHAR:
         case Types.NCHAR:
         case Types.NCLOB:
            return TYPE_STRING;
         case Types.NUMERIC:
            return TYPE_BIGDECIMAL;
         case Types.TIMESTAMP:
            return TYPE_TIMESTAMP;
         default:
            return super.getTypeName(code);
      }
   }
   @Override
   public String getHibernateTypeName(int code, int length, int precision,   int scale) throws HibernateException {
      if (LOG.isDebugEnabled()) {
         LOG.debug("getHibernateTypeName() for code \""+code+"\" with length="+length+", precision="+precision+", scale="+scale);
      }
      switch (code) {
         case Types.NVARCHAR:
            return Hibernate.STRING.getName();
         case Types.NCHAR:
            return Hibernate.CHARACTER.getName();
         case Types.NCLOB:
            return Hibernate.CLOB.getName();
         default:
            return super.getTypeName(code, length, precision, scale);
      }
   }
}

i altered the param "hibernate.dialect" in the persistence.xml, so that it points to my dialect-class.
after starting jboss and running my prgram i get following Exception in hibernate:
Code:
java.lang.NullPointerException
    at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.extract(CustomLoader.java:474)
    at org.hibernate.loader.custom.CustomLoader$ResultRowProcessor.buildResultRow(CustomLoader.java:420)
    at org.hibernate.loader.custom.CustomLoader.getResultColumnOrRow(CustomLoader.java:317)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:606)
    at org.hibernate.loader.Loader.doQuery(Loader.java:701)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
    at org.hibernate.loader.Loader.doList(Loader.java:2220)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
    at org.hibernate.loader.Loader.list(Loader.java:2099)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
    at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
    at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
    at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:80)
    at de.xxx.sql.DaoServiceImpl.findOne(DaoServiceImpl.java:3371)
    at de.xxx.sql.DaoServiceImpl.findGasVnbId(DaoServiceImpl.java:1819)
    ...


what i missed in my Dialect implementation?
i just want to convert Mapping Type -9 (NVARCHAR) to a String.

regards, Chris


Top
 Profile  
 
 Post subject: Re: Hibernate 3.6 CR2 & MSSQL - JDBC Dialect Mapping Type -9
PostPosted: Thu Oct 21, 2010 3:30 am 
Newbie

Joined: Tue Oct 19, 2010 9:18 am
Posts: 7
1st and last bump...

noone else works with latest SQL JDBC-driver (3.0), Hibernate 3.6 and MSSQL Server 2005+?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.