34

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
torno
  • 466
  • 12
  • 25
Alvin Baena
  • 903
  • 1
  • 10
  • 24
  • probably some useful info [here](https://hibernate.onjira.com/browse/HHH-468). – jtahlborn Aug 30 '12 at 16:35
  • 2
    @alvinbaena: any correct answer below?? – Adriano Oct 05 '12 at 17:10
  • I think this is a duplicate question which has been answered here : http://stackoverflow.com/questions/3383169/hibernate-jpa-mysql-and-tinyint1-for-boolean-instead-of-bit-or-char/10224905#10224905 – Donatello Dec 05 '14 at 11:01

10 Answers10

42

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;
Cyril N.
  • 38,875
  • 36
  • 142
  • 243
  • 3
    It's been a while since I asked this, and the project where this question was relevant is long ago lost. I really don't recall how I solved this, but seeing as some people have found this answer to be appropiate I'll accept it. – Alvin Baena Mar 25 '14 at 17:00
  • 1
    Yep. I had the same error but my column was a `bit`. I used this solution but with `BIT` instead of `TINYINT` in the `columnDefinition` and it worked. – nonzaprej May 18 '17 at 12:51
8

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

@Column(nullable = false, columnDefinition = "BIT", length = 1)
private boolean flag = false;
am0wa
  • 7,724
  • 3
  • 38
  • 33
5

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"

aboudirawas
  • 192
  • 3
  • 21
Mudit Verma
  • 469
  • 5
  • 3
2

Try this:

  <property name="isPaymentReceived" type="java.lang.Boolean">
  <column name="IS_PAYMENT_RECEIVED" sql-type="tinyint"/>
</property>
Piraba
  • 6,974
  • 17
  • 85
  • 135
1

I was able to solve this issue by adding "transformedBitIsBoolean=true" to my MySQL connection string.

See this question: "Found: bit, expected: boolean" after Hibernate 4 upgrade

And this forum post: https://hibernate.atlassian.net/browse/HHH-6935

Community
  • 1
  • 1
Matthias Wuttke
  • 1,982
  • 2
  • 21
  • 38
0

I ran into similar situation with hibernate today and ended up having mysql data type as tinyint(1) and declared hibernate type as boolean and it did the trick

JavaMan
  • 351
  • 1
  • 3
  • 13
0

@Type annotation is hibernate annotation to use with JPA, one can use ColumnDefiniton Attribute.

@Column(nullable = false, columnDefinition = "TINYINT(1)")
private boolean isTrue;
Ali Arda Orhan
  • 764
  • 2
  • 9
  • 24
0

What's wrong with mapping it as an int and using an accessor (isAdmin) to get the boolean value. I hope you're obscuring the actual type anyway.

Thom
  • 14,013
  • 25
  • 105
  • 185
0

Mapping it as org.hibernate.type.BooleanType might work.

See http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/types.html#types-value-basic.

Daan
  • 1,516
  • 12
  • 20
-1

Try this.

Define your column as bit(1)

CREATE TABLE test_table (bool_column BIT(1));

Define your entity property as boolean

Map the property like this

@Column(name = "bool_column", columnDefinition = "BIT")
public boolean boolField;

I think this way is simpler and in addition you stick to the jpa standard.

I have this working with MySQL and Hibernate 5.
Emiliano Schiano
  • 1,862
  • 1
  • 25
  • 30