我遇到了 hibernate 问题。我最近将我的 hbm2ddl 设置为验证,它一直在提示错误的数据类型。我已经解决了除 boolean 值之外的所有问题。
我的类中有一个字段opener
,映射为:
<property column="opener" name="opener" type="boolean"/>
列 opener
是一个 tinyint (4)
并且值为 1 或 0。到目前为止,我已经尝试更改类型,但无济于事。我还尝试在我的 hibernate.cfg 中使用以下设置:
<property name="hibernate.query.substitutions">true 1, false 0</property>
但我仍然遇到同样的错误。我做错了什么?
org.hibernate.HibernateException: Wrong column type: opener, expected: bit
at org.hibernate.mapping.Table.validateColumns(Table.java:261)
at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1083)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:116)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:317)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
注意:我无权访问数据库。
最佳答案
如果您不能更改表中的 SQL 类型,我建议您这样做:
<property name="opener" column="opener" type="path.to.your.package.YourClassUserType"/>
并创建你的类:
import org.hibernate.usertype.UserType;
public class YourClassUserType implements UserType{
...
}
您必须从接口(interface) UserType 中实现方法。 该实现会将字节转换为 boolean 值(因为 TINYINT 在 Java 中映射为字节)
祝你好运:)
关于java - 使用 hibernate 映射 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6188680/