Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map a tinyint as boolean hibernate

I have a BOOLEAN type in a MySQL table (TINYINT(1)) and I'm trying to map the boolean field in an entity but this generates an exception:

org.hibernate.HibernateException: Wrong column type in maegul.users for column admin. Found: bit, expected: boolean

I changed the field in my entity to byte and make the respective changes so it acts a boolean, and I get:

org.hibernate.HibernateException: Wrong column type in maegul.users for column admin. Found: bit, expected: tinyint

I tried using the @Type annotation on the field:

@Type(type = "org.hibernate.type.NumericBooleanType")

but I get:

org.hibernate.HibernateException: Wrong column type in maegul.users for column admin. Found: bit, expected: integer
like image 576
Alvin Baena Avatar asked Nov 07 '11 15:11

Alvin Baena


People also ask

Is Tinyint a Boolean?

There is no difference between TINYINT(1) and Boolean. The keyword Bool or Boolean internally converts into TINYINT(1) or we can say Bool or Boolean are synonymous with TINYINT(1).

Why is Boolean changed to Tinyint?

MySQL a-z in Telugu Yes, MySQL internally convert bool to tinyint(1) because tinyint is the smallest integer data type. Look at the above sample output, the column isAgeGreaterThan18 data type is converted from bool to tinyint(1) internally.

Is Tinyint Boolean in MySQL?

In MySQL, TINYINT(1) and boolean are synonymous. Because of this, the MySQL driver implicitly converts the TINYINT(1) fields to boolean if the the Java tinyInt1isBit configuration property is set to true (which is the default) and the storage size is 1.

What is difference between Tinyint and Boolean in MySQL?

The basic difference between Boolean and tinyint(1) is only in the naming convention. If we say that we need true or false values then Boolean comes to our mind, instead of tinyint(1). These data types are synonyms. It is up to us which data type we want to use- values can be 1 and 0 or true and false.


3 Answers

From what I read here :

org.hibernate.HibernateException: Wrong column type in maegul.users for column admin. Found: bit, expected: integer

It seems Hibernate is expecting an integer and got a bit.

Which mean your annotation is now correct :

@Type(type = "org.hibernate.type.NumericBooleanType") 

But maybe it has updated your database to set as Bit instead of integer, thus the error.

If you really need a TinyInt, you can use @Type AND @Column, to set as Integer, of type TinyInt :

@Column(columnDefinition = "TINYINT") @Type(type = "org.hibernate.type.NumericBooleanType") public boolean admin = true; 
like image 90
Cyril N. Avatar answered Sep 19 '22 03:09

Cyril N.


Better use BIT(1) instead of TINYINT(1)

@Column(nullable = false, columnDefinition = "BIT", length = 1)
private boolean flag = false;
like image 44
am0wa Avatar answered Sep 20 '22 03:09

am0wa


You can do it from Dialect which will not require tedious col level annotation at all places:

import org.hibernate.Hibernate;
import org.hibernate.dialect.PostgreSQLDialect;
import java.sql.Types;

public class PostgresCustomConversionDialect extends PostgreSQLDialect {

    public PostgresCustomConversionDialect() {
        super();
        this.registerColumnType( Types.BIT, "numeric(1, 0)" );
        this.registerColumnType( Types.BOOLEAN, "numeric(1, 0)" );
    }

    public String toBooleanValueString(boolean bool) {
        return bool ? "1" : "0";
    }

}

Then use this custom dialect as postgres dialect in - "hibernate.dialect"

like image 27
Mudit Verma Avatar answered Sep 17 '22 03:09

Mudit Verma