Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange Hibernate exception with column type

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.

like image 406
Jacob Smith Avatar asked Nov 03 '22 10:11

Jacob Smith


1 Answers

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

like image 128
Andreas Aumayr Avatar answered Nov 09 '22 11:11

Andreas Aumayr