我在MySQL表中有一个BOOLEAN类型(TINYINT(1)),我试图映射实体中的boolean字段,但这会产生一个异常:
org.hibernate.HibernateException: Wrong column type in maegul.users for column admin. Found: bit, expected: boolean我将实体中的字段更改为byte,并进行相应的更改,以便它充当一个布尔值,然后我得到:
org.hibernate.HibernateException: Wrong column type in maegul.users for column admin. Found: bit, expected: tinyint我尝试在字段上使用@Type注释:
@Type(type = "org.hibernate.type.NumericBooleanType")但我得到了:
org.hibernate.HibernateException: Wrong column type in maegul.users for column admin. Found: bit, expected: integer发布于 2012-02-02 19:21:30
从我在这里读到的:
org.hibernate.HibernateException:列管理员的maegul.users中的列类型错误。Found: bit,expected:整数
似乎Hibernate期望的是一个整数,但得到了一点。
这意味着您的注释现在是正确的:
@Type(type = "org.hibernate.type.NumericBooleanType")但也许它已经更新了你的数据库,将其设置为位而不是整型,从而导致错误。
如果您确实需要一个整数,您可以使用@Type和@Column来设置为TinyInt类型的TinyInt:
@Column(columnDefinition = "TINYINT")
@Type(type = "org.hibernate.type.NumericBooleanType")
public boolean admin = true;发布于 2016-03-31 21:49:27
最好使用BIT(1)而不是TINYINT(1)
@Column(nullable = false, columnDefinition = "BIT", length = 1)
private boolean flag = false;发布于 2018-02-01 20:51:25
你可以从方言开始做,它在所有地方都不需要繁琐的列级别注释:
import org.hibernate.Hibernate;
import org.hibernate.dialect.PostgreSQLDialect;
import java.sql.Types;
public class PostgresCustomConversionDialect extends PostgreSQLDialect {
public PostgresCustomConversionDialect() {
super();
this.registerColumnType( Types.BIT, "numeric(1, 0)" );
this.registerColumnType( Types.BOOLEAN, "numeric(1, 0)" );
}
public String toBooleanValueString(boolean bool) {
return bool ? "1" : "0";
}
}然后在- "hibernate.dialect“中使用这个自定义方言作为postgres方言
https://stackoverflow.com/questions/8038939
复制相似问题