Getting org.hibernate.MappingException: No Dialect

2019-01-29 08:04发布

问题:

I am getting following exception at query.list() line:

org.hibernate.MappingException: No Dialect mapping for JDBC type: -4
    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:369)
    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:1787)
    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:2211)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
    at org.hibernate.loader.Loader.list(Loader.java:2090)
    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)

following is my configuration file:

<property name="hibernate.connection.driver_resource">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">mysql</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydatabase</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.default_schema">mydatabase</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

When I am trying to run application into Eclipse IDE then this exception is not coming but when I create jar of application and run then only I am getting it. thanks in advance...

回答1:

Sometimes database returns results of custom SQL queries in strange types that cannot be mapped to Hibernate types (especially when you use expressions under select).

You need to find an offending query and add an explicit cast to it.

For example

Object o = session.createSQLQuery("select 2*2").uniqueResult();

may cause such a problem. You may fix it as follows:

Object o = session.createSQLQuery("select cast(2*2 as int)").uniqueResult();


回答2:

Got the solution:

Just change the Query, I am fetching whole record instead of select specific. e.g. from table and then get respective field (here script) value from table Object instead of using select script from table, It is working fine now.