Java 用于布尔值而非位或字符的休眠 JPA、MySQL 和 TinyInt(1)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3383169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:47:19  来源:igfitidea点击:

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

javamysqlhibernatejpajpa-2.0

提问by Ta Sas

Here is my JPA2 / Hibernate definition:

这是我的 JPA2/Hibernate 定义:

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?

在 MySql 中,此列被解析为 bit(1) 数据类型 - 这对我不起作用。对于遗留问题,我需要将布尔值映射到 tinyint,而不是一点点。但我看不到更改默认数据类型的可能性。有没有?

采纳答案by Mike Q

Try the NumericBooleanType. For some reason this doesn't have a declared short type name so you'd have to use:

试试吧NumericBooleanType。出于某种原因,它没有声明的短类型名称,因此您必须使用:

@Column(nullable = false)
@Type(type = "org.hibernate.type.NumericBooleanType")
private boolean enabled;

This does map to an INTEGER type but it will probably work fine with a TINYINT.

这确实映射到 INTEGER 类型,但它可能适用于 TINYINT。

UPDATE: org.hibernate.type.NumericBooleanTypeDoes notwork with TINYINT in some RDBMS. Switch the database column type to INTEGER. Or use a different Java @Type value, or columnDefinition, as appropriate.

UPDATE:org.hibernate.type.NumericBooleanType不会不会在一些RDBMS与TINYINT工作。将数据库列类型切换为 INTEGER。或者根据需要使用不同的 Java @Type 值或 columnDefinition。

In this example, Dude's answer of @Column(nullable = false, columnDefinition = "TINYINT(1)")would work without any database changes.

在这个例子中,Dude 的答案@Column(nullable = false, columnDefinition = "TINYINT(1)")将在没有任何数据库更改的情况下工作。

回答by Donatello

@Type annotation is an Hibernate annotation.

@Type 注解是一个 Hibernate 注解。

In full JPA2 (with Hibernate 3.6+), the way to map a Boolean field to a TINYINT(1) SQL type instead of BIT(1), is to use the columnDefinition attribute.

在完整的 JPA2(使用Hibernate 3.6+)中,将布尔字段映射到 TINYINT(1) SQL 类型而不是 BIT(1) 的方法是使用 columnDefinition 属性。

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

nb: length attribute seems to have no effect in this case, then we use (1) syntax.

nb: length 属性在这种情况下似乎没有作用,那么我们使用 (1) 语法。



With Hibernate 4.0+, this kind of syntax can cause an runtime error like this :

使用Hibernate 4.0+,这种语法可能会导致运行时错误,如下所示:

Wrong column type Found: bit, expected: TINYINT(1)

It seems that in this case, your only way is to use tinyInt1isBit=falsein the MySQL datasource connection string like this :

似乎在这种情况下,您唯一的方法是在 MySQL 数据源连接字符串中使用tinyInt1isBit=false,如下所示:

jdbc:mysql://server_host:3306/database?tinyInt1isBit=false

By the way, you can now use the length attribute like this :

顺便说一句,您现在可以像这样使用 length 属性:

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

回答by marioarranzr

I had this error:

我有这个错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/config/context-config.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: org.hibernate.type.NumericBooleanType, at table: bookingItem, for columns: [org.hibernate.mapping.Column(enabled)]

引起:org.springframework.beans.factory.BeanCreationException:在ServletContext资源[/WEB-INF/config/context-config.xml]中定义名称为'sessionFactory'的bean创建错误:初始化方法调用失败;嵌套异常是 org.hibernate.MappingException:无法确定类型:org.hibernate.type.NumericBooleanType,在表:bookingItem,对于列:[org.hibernate.mapping.Column(enabled)]

And this worked for me:

这对我有用:

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

回答by Annulet Consulting

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

我在 MySQL 数据库上使用 JPA 和 Spring Data/Hibernate 5.0。

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).

我的开发环境将休眠自动 ddl 设置为更新,因此当我部署到开发时,它创建了带有 tinyint(1) 类型的 column_name 的表。

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.

我使用此列的代码将 null 视为 false,因此我不担心 null,如果是,您可以将其设为原始布尔值或在 Column 注释中添加“, nullable = false”。

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

此解决方案完全是 JPA(不使用休眠类型注释)并且不需要更改连接字符串。

回答by Nox

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

使用 Microsoft sql 和某些版本的 mysql 时,请使用以下内容:

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

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

对我来说,tinybit、boolean 和其他这样的定义失败了。

回答by BlueBird

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.

我正在使用 Spring Data JPA 2.2.5。当我使用相同的代码库同时使用 MySQL 和 MariadDB 时,我遇到了类似的问题。它在一个上起作用,在另一个上不起作用。

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

问题是,我将字段创建为未签名。

I moved the below SQL part from

我将下面的 SQL 部分从

`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...

这解决了这个问题,并且在 Mysql 和 MariaDB 中都可以正常工作,没有任何问题......