Hibernate SQL QUERY, problem with TEXT data type i

2020-07-26 02:39发布

问题:

I need to perform a sql query with hibernate ( no mapping ) but i keep getting this error whenever a field has a TEXT data type in MYSQL :

org.hibernate.MappingException: No Dialect mapping for JDBC type: -1

I don't know what to do, mapping is not an option ( dynamic tables in the database , so the number of fields is variable ).

Here's the piece of code :

SQLQuery query = session.createSQLQuery(sql);

Object[] values = (Object[]) query.uniqueResult();

sql is a String containing the query ( it runs ok with mysql query engine ). If I change the TEXT data type to varchar, works fine, but that's not an option neither !

Any clues ?

回答1:

Here's a possible solution:

package iam.dirty;
import java.sql.Types;

import org.hibernate.Hibernate;
import org.hibernate.dialect.SQLServerDialect;

public class DialectForGkoloc extends SQLServerDialect {
     public DialectForGkoloc() {
        super();

        registerHibernateType(Types.DECIMAL, Hibernate.BIG_DECIMAL.getName());   
        registerHibernateType(-1, Hibernate.STRING.getName());
        registerHibernateType(Types.LONGVARCHAR, Hibernate.TEXT.getName());
     }

}

Modify the Hibernate configuration file hibernate.cfg.xml:

<property name="dialect"> 
 org.hibernate.dialect.SQLServerDialect 
</property>

with:

<property name="dialect"> 
 iam.dirty.DialectForGkoloc 
</property>


回答2:

A quick google (you did try this first, right?) suggest addScalar is your friend here.