Hibernate, HSQL, Native Queries and Booleans

I use an in-memory HSQL Database in order to test my JPA/Hibernate Application. Common thing. That explains my surprise on the following situation: I have an Entity with a boolean member:

                    @Entity public class Foo{ @Id int id; boolean bar; [..] }
                

Everything works as expected up to here. But then, i use a native query like this:

                    SELECT id,bar FROM Foo f
                

Imagine the look of my face when i got this in response:

                    Caused by: org.hibernate.MappingException: No Dialect mapping for JDBC type: 16
                

while Type 16 is boolean. WTF? After a little digging, the Hibernate-Dialect is found to be incomplete, for it does not know
how to map booleans in native queries’
I don’t get it. I cannot be the first guy running into this. Anyway, i’ll file a bug for the hibernate guys (let’s see if they smack me down for that) and wait and see.

Until then, you can easily work around this by using this extension:

                    public class CustomHSQLDialect extends org.hibernate.dialect.HSQLDialect
{
    public CustomHSQLDialect()
    {
        registerColumnType(Types.BOOLEAN, "boolean");
        registerHibernateType(Types.BOOLEAN, "boolean");
    }
}
                

and then use this Dialect in you persistence.xml instead of org.hibernate.dialect.HSQLDialect.

3 comments to Hibernate, HSQL, Native Queries and Booleans

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>