32

I'm trying to upgrade from Hibernate 3.6.5 to 4.0 (and from Spring 3.0.5 to 3.1 which is required for Hibernate 4 support).

Now, with both MySQL and HSQL, I'm running into this problem with persistent boolean fields:

Caused by: org.hibernate.HibernateException: 
Wrong column type in PUBLIC.PUBLIC.EVENT for column Checked. Found: bit, expected: boolean
    at org.hibernate.mapping.Table.validateColumns(Table.java:282)
    at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1268)
    at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:155)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:453)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:184)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:314)

JPA @Entity and @Column annotations are used in domain objects, and the problematic fields look like this:

@Column(name = "Checked")
private boolean checked;

HSQL schema:

Checked bit default 0 not null,

MySQL schema:

`Checked` tinyint(1) NOT NULL default '0',

What is the most straightforward way to solve this while sticking with Hibernate 4? Should I change the database schema, Hibernate configs, or domain class annotations?

I have no idea if the code and configuration was fully "correct" before, but at least it worked fine with Hibernate 3.

fredt
  • 22,590
  • 3
  • 37
  • 60
Jonik
  • 74,291
  • 66
  • 249
  • 356
  • With HSQL, changing "bit" to "boolean" in the schema file *seems* to help (i.e., I then run into a different Hibernate 4 problem). That's a bit strange though, as [HSQL documentation](http://hsqldb.org/doc/guide/ch09.html#datatypes-section) gives the impression that BOOLEAN and BIT are equivalent. – Jonik Dec 29 '11 at 13:24

5 Answers5

46

I worked this out by adding columnDefinition = "BIT" to the @Column line.

@Basic
@Column(name = "B", columnDefinition = "BIT", length = 1)
public boolean isB() {
    return b;
}

Its defined as a 'BIT(1)' in the DB as well. Also worked with TINYINT. This is the easiest solution I've found since the change is super-minor and no need to touch the DB.

Using: MySQL Server 5.5.13, Hibernate 4.1.1, JDK 1.6

Jonik
  • 74,291
  • 66
  • 249
  • 356
baba smith
  • 689
  • 7
  • 16
  • 2
    No longer needed in Hibernate version 4.3.0+ – Ishmael Jul 16 '15 at 19:57
  • this solution will work - however it's not a fix for a root problem - which is mysql server itself - consider adding 'transformedBitIsBoolean' flag to your datasource URL as suggested in https://stackoverflow.com/a/22200375/1694963 – Miron Balcerzak Aug 15 '19 at 14:19
20

I had the same problem and I extended the Dialect to take into account the fact that mysql treats boolean as an alias of bit.

public class Mysql5BitBooleanDialect extends MySQL5Dialect{     
    public Mysql5BitBooleanDialect() {
        super();
        registerColumnType( java.sql.Types.BOOLEAN, "bit" );        
    }       
}

I don't use longer bit() fields (to represent for example byte[]) so this might break that.

n00begon
  • 3,467
  • 3
  • 26
  • 41
Michal Stawski
  • 201
  • 2
  • 3
  • 2
    This seems like (another) big type-related oversight in Hibernate's MySQL dialect. I put off upgrading when 4.0 was released and I'm surprised it hasn't been addressed a year later. But thanks for the tip. Extending the dialect and updating persistence.xml seems much better than adding a columnDefinition any time I annotate a boolean field. I'm using Hibernate as a JPA implementation and I want to keep the non-configuration bits as implementation-neutral as possible. – spaaarky21 Jan 01 '13 at 04:43
  • 1
    Yet 6 months later the problem is still there. The worst is that the schema generated by hibernate2ddl itself uses boolean with MySQL. So the Hibernate-generated schema cannot be validated by Hibernate :( – Pierre Henry Jun 25 '13 at 09:50
  • 2
    At last it should be corrected in the next release (4.3.0): https://hibernate.atlassian.net/browse/HHH-6935 – TahitianGabriel Jul 24 '13 at 01:08
10

I was able to solve this issue by adding transformedBitIsBoolean=true to my MySQL connection string. See here: https://hibernate.atlassian.net/browse/HHH-6935

Jonik
  • 74,291
  • 66
  • 249
  • 356
Matthias Wuttke
  • 1,887
  • 2
  • 18
  • 36
0

This has been answered in a similar question here :

Hibernate JPA, MySQL and TinyInt(1) for Boolean instead of bit or char

Your problem could be a bit more complicated as you use HSQL DB at the same time, but you can take a look and try it anyway !

Community
  • 1
  • 1
Donatello
  • 2,597
  • 2
  • 22
  • 28
-1

Found the problem i also got org.hibernate.HibernateException: Wrong column type ... Found: bit, expected: boolean

on BooleanType in hibernate 4 they changed the Ctor to

public BooleanType() {
    this( org.hibernate.type.descriptor.sql.BooleanTypeDescriptor.INSTANCE, BooleanTypeDescriptor.INSTANCE );
}

instead of old versions

public BooleanType() {
    this( BitTypeDescriptor.INSTANCE, BooleanTypeDescriptor.INSTANCE );
}
n00begon
  • 3,467
  • 3
  • 26
  • 41
orasio
  • 9
  • Um, so what is the solution? Quoting my question: "What is the most straightforward way to solve this while sticking with Hibernate 4? Should I change the database schema, Hibernate configs, or domain class annotations?" – Jonik Jan 12 '12 at 14:17
  • it's not the answer to the question. – Miron Balcerzak Aug 15 '19 at 14:19