Hibernate JPA, MySQL and TinyInt(1) for Boolean instead of bit or char

Viewed 109997

Here is my JPA2 / Hibernate definition:

Code:
@Column(nullable = false)
private boolean enabled;

In MySql this column is resolved to a bit(1) datatype - which does not work for me. For legacy issues I need to map the boolean to a tinyint not to a bit. But I do not see a possibility to change the default datatype. Is there any?

6 Answers

I'm using JPA with Spring Data/Hibernate 5.0 on a MySQL database.

In my Entity object, I put the following:

@Column(name = "column_name", columnDefinition = "BOOLEAN")
private Boolean variableName;

My dev environment has hibernate auto-ddl set to update, so when I deployed to dev, it created the table with column_name of type tinyint(1).

My code that uses this column considers null as false, so I'm not worried about nulls, if you are, you could make it a primitive boolean or add ", nullable = false" to the Column annotation.

This solution is fully JPA (doesn't use hibernate Type annotation) and requires no change to the connection string.

When using Microsoft sql and some versions of mysql use the following:

@Column(name = "eanbled", columnDefinition = "bit default 0", nullable = false)
private boolean enabled;

For me, tinybit, boolean, and other such definitions failed.

Old question but probably will save someone's time.

I am using Spring Data JPA 2.2.5. I had a similar issue when I work with MySQL and MariadDB parallelly with the same code base. It worked on one and didn't on another.

The issue was, I was creating the field as unsigned.

I moved the below SQL part from

`is_fixed` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',

to the below

`is_fixed` TINYINT(1) NOT NULL DEFAULT '0',

this fixed the issue and was working in both Mysql and MariaDB without any issue...

Related