Alvin Baena
Alvin Baena

Reputation: 903

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

Upvotes: 34

Views: 75230

Answers (10)

Emiliano Schiano
Emiliano Schiano

Reputation: 1912

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.

Upvotes: -1

Mudit Verma
Mudit Verma

Reputation: 469

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"

Upvotes: 5

am0wa
am0wa

Reputation: 8377

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

@Column(nullable = false, columnDefinition = "BIT", length = 1)
private boolean flag = false;

Upvotes: 8

Ali Arda Orhan
Ali Arda Orhan

Reputation: 784

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

@Column(nullable = false, columnDefinition = "TINYINT(1)")
private boolean isTrue;

Upvotes: 1

Matthias Wuttke
Matthias Wuttke

Reputation: 2032

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

Upvotes: 1

JavaMan
JavaMan

Reputation: 351

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

Upvotes: 0

Piraba
Piraba

Reputation: 7014

Try this:

  <property name="isPaymentReceived" type="java.lang.Boolean">
  <column name="IS_PAYMENT_RECEIVED" sql-type="tinyint"/>
</property>

Upvotes: 2

Cyril N.
Cyril N.

Reputation: 39859

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;

Upvotes: 42

Daan
Daan

Reputation: 1546

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.

Upvotes: 0

Thom
Thom

Reputation: 15042

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.

Upvotes: 0

Related Questions