Wednesday, September 15, 2010

Hibernate SQLQuery

In the last week I have seen this error twice when running native SQL queries to a MySQL database using Hibernate. (I am forced to use SQL - not my first choice.)
No Dialect mapping for JDBC type: -1

It took me a while to find the answer, so I thought I would share.

The problem this time is that the data I was retrieving was a longtext column (not a regular varchar) and this is not automatically mapped by the MySQL dialect (I believe this is fixed in Hibernate 3.5).

I solved the problem by using "addScalar" to define the mapping to use. It worked!


Query query = getSession()
.createSQLQuery("select data from form_data where form_data_id = ?")
.addScalar("data", Hibernate.TEXT)
.setInteger(0, formData.getId());
String oldData = ((String)query.uniqueResult());

No comments:

Post a Comment