5

I am getting Hibernate exception:

Wrong column type. Found: bit, expected: BOOLEAN DEFAULT TRUE

I have class User:

package mypackage;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "USER")
public class User {

   @Id
   @GeneratedValue
   @Column(name = "ID", unique = true, nullable = false)
   private Long id;

   @Column(name = "STATUS", columnDefinition = "BOOLEAN DEFAULT TRUE", 
                               nullable = false)
   private Boolean status = Boolean.TRUE;

   // getters and setters
}

This works properly if Hibernate creates table itself in database. When

<property name="hibernate.hbm2ddl.auto">create</property>

or

<property name="hibernate.hbm2ddl.auto">create-drop</property>

But if

  1. I run my program and allow Hibernate to create this table
  2. then I change the value of hibernate.hbm2ddl.auto to the validate
  3. and I run my program again with the table that has generated by Hibernate and with

    <property name="hibernate.hbm2ddl.auto">validate</property>
    

so I get the exception:

org.hibernate.HibernateException: Wrong column type in dbtest.user for column STATUS. Found: bit, expected: BOOLEAN DEFAULT TRUE

Any idea what could be the reason for this behavior of Hibernate and how can I fix it?

I use MySQL server 5.1 and Hibernate 4.0.1.

My Run class is just two lines:

 public class Run {

   public static void main(String[] main) {

     SessionFactory sessionFactory = 
                new AnnotationConfiguration().configure().buildSessionFactory();

     Session session = sessionFactory.getCurrentSession();
   }
 }

The structure of the table:

CREATE TABLE USER (
    ID BIGINT(20) NOT NULL AUTO_INCREMENT,
    STATUS TINYINT(1) NOT NULL DEFAULT 1,
    PRIMARY KEY (ID),
    UNIQUE INDEX ID (ID)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=2

My hibernate.cfg:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://127.0.0.1:3306/dbtest</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>

    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="current_session_context_class">thread</property>
    <property name="connection.pool_size">5</property>
    <property name="hibernate.transaction.flush_before_completion">true</property>
    <property name="hibernate.transaction.auto_close_session">true</property>
    <property name="hibernate.hbm2ddl.auto">validate</property>
    <property name="hibernate.show_sql">false</property>
    <property name="hibernate.format_sql">true</property>
    <property name="hibernate.use_sql_comments">false</property>
    <property name="hibernate.connection.charSet">true</property>

    <mapping class="mypackage.User"/>
  </session-factory>
</hibernate-configuration>

Hibernate creates the table with type TINYINT and I do not intermeddle on to the database and do not make any changes in the table manually! I'm just change the hibernate.hbm2ddl.auto to the validate and nothing else.

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
Jacob Smith
  • 51
  • 1
  • 3

3 Answers3

2

As the other guys already said: There is no boolean-column type in MySql. I had a similar problem and "created" an own dialect. It extends the default MySQL5Dialect and registers the stanard sql-type BOOLEAN to the MySql column type bit.

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

You can then use it by setting the dialect in your hibernate properties: hibernate.dialect=your.package.Mysql5BitBooleanDialect

Hope this helps..

Andreas Aumayr
  • 1,006
  • 1
  • 11
  • 17
0
private Boolean status = Boolean.TRUE;

Should result in a default value of true already.

Another way of solving this problem is using:

@PrePersist
public void prePersist()
{
    if ( status == null )
    {
         status = Boolean.TRUE;
    }
}
Aidamina
  • 1,894
  • 20
  • 14
0

Wild guess since I haven't used annotations form mapping, but here goes:

That columnDefinition looks like explicitly telling hibernate to use boolean column type with default value true, but MySql doesn't have boolean type, so maybe

columnDefinition = "BOOLEAN DEFAULT TRUE"

should be something like this

columnDefinition = "tinyint DEFAULT 1"
Bloodboiler
  • 2,082
  • 2
  • 24
  • 21