Hibernate JPA,MySQL和TinyInt(1)用于Boolean而不是bit或char [英] Hibernate JPA, MySQL and TinyInt(1) for Boolean instead of bit or char

查看:2179
本文介绍了Hibernate JPA,MySQL和TinyInt(1)用于Boolean而不是bit或char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 代码:
@Column(nullable = false)
私有布尔启用;

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

解决方案

尝试 NumericBooleanType 。出于某种原因,这没有一个声明的短型名称,所以你必须使用:

  @Column(nullable =假)
@Type(type =org.hibernate.type.NumericBooleanType)
私有布尔启用;

它映射到INTEGER类型,但它可能在TINYINT下正常工作。


$ b

UPDATE: org.hibernate.type.NumericBooleanType 在某些RDBMS中, 不适用于TINYINT。将数据库列类型切换为INTEGER。或者使用不同的Java @Type值或columnDefinition。



在此示例中,Dude的答案是 @Column(nullable = false, columnDefinition =TINYINT(1))可以在没有任何数据库更改的情况下工作。


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?

解决方案

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

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

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

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

这篇关于Hibernate JPA,MySQL和TinyInt(1)用于Boolean而不是bit或char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆