mysql tinyint hibernate_java-Hibernate JPA,MySQL和TinyInt(1)的布尔值,而不是bit或ch

java-Hibernate JPA,MySQL和TinyInt(1)的布尔值,而不是bit或ch

这是我的JPA2 / Hibernate定义:

Code:

@Column(nullable = false)

private boolean enabled;

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

5个解决方案

63 votes

@Type注释是一个Hibernate注释。

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

@Column(nullable = false, columnDefinition = "TINYINT(1)")

private boolean enabled;

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

在Hibernate 4.0+中,这种语法会导致如下运行时错误:

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

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

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

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

@Column(nullable = false, columnDefinition = "TINYINT", length = 1)

private boolean enabled;

Donatello answered 2020-07-05T06:29:36Z

37 votes

尝试@Column(nullable = false, columnDefinition = "TINYINT(1)")。由于某种原因,它没有声明的短类型名称,因此您必须使用:

@Column(nullable = false)

@Type(type = "org.hibernate.type.NumericBooleanType")

private boolean enabled;

这确实映射到INTEGER类型,但可能与TINYINT一起正常工作。

更新:@Column(nullable = false, columnDefinition = "TINYINT(1)")在某些RDBMS中不适用于TINYINT。 将数据库列类型切换为INTEGER。 或根据需要使用其他Java @Type值或columnDefinition。

在此示例中,Dude的@Column(nullable = false, columnDefinition = "TINYINT(1)")答案无需更改任何数据库即可工作。

Mike Q answered 2020-07-05T06:28:54Z

3 votes

我在MySQL数据库上将JPA与Spring Data / Hibernate 5.0一起使用。

在我的Entity对象中,输入以下内容:

@Column(name = "column_name", columnDefinition = "BOOLEAN")

private Boolean variableName;

我的开发环境将hibernate auto-ddl设置为要更新,因此当我部署到开发环境时,它创建的表的column_name类型为tinyint(1)。

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

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

Annulet Consulting answered 2020-07-05T06:30:14Z

2 votes

我有这个错误:

引起原因:org.springframework.beans.factory.BeanCreationException: 创建名称为“ sessionFactory”的bean时出错 ServletContext资源[/WEB-INF/config/context-config.xml]: 调用init方法失败; 嵌套异常为 org.hibernate.MappingException:无法确定以下类型: org.hibernate.type.NumericBooleanType,在表:bookingItem上,用于 列:[org.hibernate.mapping.Column(enabled)]

这对我有用:

@Column(nullable = false, columnDefinition = "TINYINT(1)")

private boolean enabled;

marioarranzr answered 2020-07-05T06:30:44Z

0 votes

当使用Microsoft sql和某些mysql版本时,请使用以下命令:

@Column(name = "eanbled", columnDefinition = "bit default 0", nullable = false)

private boolean enabled;

对我来说,tinybit,布尔值和其他此类定义都失败了。

Nox answered 2020-07-05T06:31:08Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Universal Data Access Components History [email protected] 7.4.12 21-Jan-19 Bug with using the datetime literals in SQL statements in TVirtualQuery is fixed Bug with AV failure in the Refresh method in RAD Studio 10.2 Tokyo on Linux is fixed Oracle data provider Bug with authentication of old users in Oracle 12c is fixed Bug with the DML arrays that contain the TIMESTAMP values is fixed Bug with describe of SYNONYM for stored procedures in the Direct mode is fixed Bug with fetch data when the FieldsAsString option is set to True in the Direct mode is fixed Bug with open REFCURSOR is fixed SQLServer data provider Bug with the "Cannot modify a read-only dataset" error in Android is fixed MySQL data provider Bug with setting the data type of the parameter to BOOLEAN for all TINYINT system data types in the functions is fixed PostgreSQL data provider Bug with reading and writing of the floating point values is fixed InterBase data provider Bug with using the AsBytes property is fixed Bug with the AV failure when using the BLOB data type in Lazarus is fixed SQLite data provider Now the Direct mode is based on the SQLite engine version 3.26.0 DBF data provider Bug with wrong selection of deleted records is fixed Bug with storing numeric values is fixed Bug with using the CTOD, DTOC, DTOS, TTOC, TTOD and EMPTY functions in index expressions is fixed Bug with using a field named RECNO in the index expressions is fixed MongoDB data provider Bug with reading string values in the BSON format is fixed NexusDB data provider Bug with missing files for NexusDB for RAD Studio Rio (10.3) is fixed ODBC data provider The UnknownAsSring specific option is added

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值