Getting Exception No Dialect mapping for JDBC type

2019-04-26 19:00发布

问题:

I am new to hibernate and java. I'm trying to execute native sql query but I have been stuck. Can anyone can help me out or check where I'm doing mistake?

My java code is :

try {
            trns = session.beginTransaction();
            String sql = "select principle,interest from salaryinfo where empid = " + selectedempid + "";
            SQLQuery query = session.createSQLQuery(sql);
            List<Object[]> rows = query.list();
            for (Object[] row : rows) {
                Salaryinfo si= new Salaryinfo();
                si.setPrinciple(Float.parseFloat(row[0].toString()));
                si.setInterest(Float.parseFloat(row[1].toString()));
                sis.add(si);
            }

Error Trace:

Severe:   org.hibernate.MappingException: No Dialect mapping for JDBC type: 7
    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:192)
    at org.hibernate.loader.custom.CustomLoader.getHibernateType(CustomLoader.java:161)
    at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:131)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1678)
    at org.hibernate.loader.Loader.doQuery(Loader.java:662)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
    at org.hibernate.loader.Loader.doList(Loader.java:2145)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
    at org.hibernate.loader.Loader.list(Loader.java:2024)
    .
    .
    .
    .

I did debug my program and found my program is breaking is on this line SQLQuery query = session.createSQLQuery(sql);

Hibernate config file :

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">****</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/empdata</property>
        <property name="hibernate.connection.username">****</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

        <property name="hibernate.show_sql">false</property>
    <property name="hibernate.format_sql">false</property>
    <property name="hibernate.cache.use_query_cache">true</property>
    <property name="hibernate.c3p0.acquire_increment">1</property> 
    <property name="hibernate.c3p0.idle_test_period">10</property> <!-- seconds -->             
        <mapping resource="hibernate/Salaryinfo.hbm.xml" />   

    </session-factory>
</hibernate-configuration>

回答1:

You need to put addScalar(); in your native sqlquery syntax. To avoid the overhead of using ResultSetMetadata, or simply to be more explicit in what is returned, one can use addScalar().

Like this:

SQLQuery query = session.createSQLQuery(sql)
  .addScalar("principle", new FloatType())
  .addScalar("interest", new FloatType());


回答2:

Your database is using REAL data type, but your dialect does not understand it, and hence can't map it to one of the Java types. See Types.java.