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
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).
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.
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.
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.
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;
Better use BIT(1)
instead of TINYINT(1)
@Column(nullable = false, columnDefinition = "BIT", length = 1)
private boolean flag = false;
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With