“Found: bit, expected: boolean” after Hibernate 4 upgrade

可紊 提交于 2019-11-27 01:15:45

问题


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.


回答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




回答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.




回答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




回答4:


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 !




回答5:


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 );
}


来源:https://stackoverflow.com/questions/8667965/found-bit-expected-boolean-after-hibernate-4-upgrade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!