将一个tinyint映射为布尔型休眠 [英] Map a tinyint as boolean hibernate
问题描述
我在MySQL表中有一个BOOLEAN类型(TINYINT(1)),我试图在实体中映射boolean字段,但这会产生一个异常:
org.hibernate.HibernateException:列admin中maegul.users中的列类型错误。发现:位,期望:布尔
我将实体中的字段更改为字节并进行相应更改所以它的行为是一个布尔值,我得到:
org.hibernate.HibernateException:列admin中maegul.users中的列类型错误。发现:bit,expected:tinyint
我试着使用 @Type <
$ b
@Type(type =org.hibernate.type.NumericBooleanType)
但我得到:
org.hibernate.HibernateException:列admin中maegul.users中的列类型错误。发现:位,期望:整数
这里:
org.hibernate.HibernateException:列admin中maegul.users中的列类型错误。发现:位,期望:整数
看来Hibernate期待一个整数并得到了一点。
这意味着您的注释现在是正确的:
@Type(type =org.hibernate.type .NumericBooleanType)
但是,也许它已经更新了您的数据库设置为位而不是整数,错误。
如果你确实需要一个TinyInt,你可以使用 @Type
AND @柱
,将其设置为整型类型TINYINT的:
@Column(columnDefinition = TINYINT )
@Type(type =org.hibernate.type.NumericBooleanType)
public boolean admin = true;
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
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;
这篇关于将一个tinyint映射为布尔型休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!