使用hibernate映射布尔值

Ter*_*ego 7 java hibernate hbm hbm2ddl

我遇到了hibernate的问题.我最近将我的hbm2ddl设置为validate,并且它一直在抱怨错误的数据类型.除了布尔语,我已解决了所有问题.

opener我的班级中有一个字段,映射为:

<property column="opener" name="opener" type="boolean"/>
Run Code Online (Sandbox Code Playgroud)

opener是a tinyint (4)并且值为1或0.到目前为止,我已尝试更改类型,但无济于事.我也尝试在hibernate.cfg中使用以下设置:

<property name="hibernate.query.substitutions">true 1, false 0</property>
Run Code Online (Sandbox Code Playgroud)

但我仍然得到同样的错误.我究竟做错了什么?

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)
Run Code Online (Sandbox Code Playgroud)

注意:我无法访问数据库.

小智 4

如果您无法更改表中的 SQL 类型,我建议您这样做:

<property name="opener" column="opener" type="path.to.your.package.YourClassUserType"/>
Run Code Online (Sandbox Code Playgroud)

并创建你的课程:

import org.hibernate.usertype.UserType;

public class YourClassUserType implements UserType{
 ...
}
Run Code Online (Sandbox Code Playgroud)

您必须实现 UserType 接口中的方法。该实现会将字节转换为布尔值(因为 TINYINT 在 Java 中映射为字节)

请参阅此处的示例

祝你好运 :)