"Found: bit, expected: boolean" after Hibernate 4 upgrade

27,712

Solution 1

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

Solution 2

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.

Solution 3

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

Share:
27,712
Jonik
Author by

Jonik

I'm a generalist software developer who's been programming professionally for 15+ years. Simplicity, code maintainability, easy deployments, automated testing, and continuous integration are some of the things close to my heart as a developer. I currently work as a freelance developer, focused on backend (Java/Scala/JVM mostly) and Android development. Enjoying Kotlin and Jetpack Compose nowadays especially! You can reach me by email through <jonik at iki dot country code for finland>.

Updated on July 12, 2020

Comments

  • Jonik
    Jonik almost 4 years

    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.