简体   繁体   中英

Hibernate dialect

I am using Hibernate to accessing data from sql server 2008.while executing the following code

Session session = sessionFactory.openSession();
String qry="select Form_Id,SUBSTRING( 
       (SELECT ( '' + t2.Form_Layout_Txt) FROM Form_Layout_Info t2  
            WHERE t1.Form_Id = t2.Form_Id " +
            " GROUP BY Form_Layout_Txt FOR XML path('') ), 1,1000000000) 
             FROM Form_Layout_Info t1 GROUP BY  Form_Id";

SQLQuery query = session.createSQLQuery(qry);
recordList = query.list();

My Hibernate properties is

hibernate.connection.driver_class=com.microsoft.sqlserver.jdbc.SQLServerDriver
hibernate.connection.url=jdbc:sqlserver://localhost:1433;databaseName=abc;integratedSecurity=false;
hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.connection.username=sa
hibernate.connection.password=p123asc

i received the following error

No Dialect mapping for JDBC type: -9 

How to fix the issue.

You need to provide hibernate.dialect=org.hibernate.dialect.SQLServerDialect in configuration for sql server.

Either provide it in hibernate.cfg.xml as :

<hibernate-configuration>
    <session-factory name="session-factory">
        .....
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        ....
 </session-factory>
</hibernate-configuration>

or in properties file as :

hibernate.dialect=org.hibernate.dialect.SQLServerDialect

I don't know which way you are using, so posted both I know.

I found 2 solutions for your problem here :

  1. try changing your driver to jTDS using the ms sql server jdbc driver provided by microsoft can cause this issue while jTDS does not give this complaint.

  2. you may need to explicitly “addScalar” to your hibernate query. You might have something like this:

    sess.createSQLQuery("SELECT * FROM CATS"); // try changing to: sess.createSQLQuery("SELECT * FROM CATS") .addScalar("ID", Hibernate.LONG) .addScalar("NAME", Hibernate.STRING) .addScalar("BIRTHDATE", Hibernate.DATE)

This issue is related to type mapping done while returning data. And mapping for that particular data type not exists in Dialect.

https://forum.hibernate.org/viewtopic.php?f=1&t=959583

Hibernate Data types -

http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/type/StandardBasicTypes.html

The error means hibernate doesn't know how to map the JDBC type “NVARCHAR” to a hibernate type.

Solution 1: Using addScalar like this:

Session session = sessionFactory.openSession();
String qry="select Form_Id,SUBSTRING( 
   (SELECT ( '' + t2.Form_Layout_Txt) as formLayoutTxt FROM Form_Layout_Info t2  
        WHERE t1.Form_Id = t2.Form_Id " +
        " GROUP BY Form_Layout_Txt FOR XML path('') ), 1,1000000000) 
         FROM Form_Layout_Info t1 GROUP BY  Form_Id";

SQLQuery query = session.createSQLQuery(qry).addScalar("formLayoutTxt", StringType.INSTANCE);
recordList = query.list();

Solution 2: Register the Hibernate Type in Dialect

public class MySQLServerDialect extends SQLServerDialect {

  public MySQLServerDialect() {
    super();

    // Register mappings
    registerHibernateType(Types.NVARCHAR, StringType.INSTANCE.getName());
  }
}

For more details, check out this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM