7

我遇到了休眠问题。我最近将我的 hbm2ddl 设置为验证,它一直在抱怨错误的数据类型。除了布尔值,我已经解决了所有问题。

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

<property column="opener" name="opener" type="boolean"/>

该列opener是 a 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)

注意:我无权访问数据库。

4

6 回答 6

4

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

<property name="opener" column="opener" type="path.to.your.package.YourClassUserType"/>

并创建您的课程:

import org.hibernate.usertype.UserType;

public class YourClassUserType implements UserType{
 ...
}

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

在此处查看示例

祝你好运 :)

于 2011-06-01T15:55:29.627 回答
4

对于遇到与我相同的麻烦的任何人,我使用了此处发布的两个答案的组合。

我实现了一个自定义用户类型来处理我的 tinyint 字段:

public class TinyIntegerToBoolean implements UserType {

    public int[] sqlTypes() {
        return new int[]{Types.TINYINT};
    }

    public Class returnedClass() {
        return Boolean.class;
    }

    public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor si, Object owner) throws HibernateException, SQLException {
        return (rs.getByte(names[0]) != 0);
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor si) throws HibernateException, SQLException {
        st.setByte(index, Boolean.TRUE.equals(value) ? (byte) 1 : (byte) 0);
    }

    /* boilerplate... */
    public boolean isMutable() {
        return false;
    }

    public boolean equals(Object x, Object y) throws HibernateException {
        if (x == null || y == null) {
            return false;
        } else {
            return x.equals(y);
        }
    }

    public int hashCode(Object x) throws HibernateException {
        assert (x != null);
        return x.hashCode();
    }

    public Object deepCopy(Object value) throws HibernateException {
        return value;
    }

    public Object replace(Object original, Object target, Object owner)
            throws HibernateException {
        return original;
    }

    public Serializable disassemble(Object value) throws HibernateException {
        return (Serializable) value;
    }

    public Object assemble(Serializable cached, Object owner)
            throws HibernateException {
        return cached;
    }
}

然后我将以下内容添加到我的映射中:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <typedef class="com.test.model.TinyIntegerToBoolean" name="tinyint_boolean"/>
</hibernate-mapping>

然后在我的开场白中我使用type=tinyint_boolean它,它就像一个魅力:)

于 2012-02-20T00:28:23.477 回答
3

您可以将 DB 列定义为 achar(1)并在 Hibernate 映射文件中将属性定义为type="yes_no",这是一个 Java 布尔类型。这些将在数据库中显示为YN值。

如果要使用tiny_int,则大小必须为 1,但我不能 100% 确定它是如何映射到 HBM 文件中的。

于 2011-05-31T14:12:39.240 回答
1

试试这个 :

<property column="opener" name="opener" access="field" />

假设你有一个吸气剂

 boolean isOpener() ;

和一个二传手

void setOpener(boolean b);
于 2011-05-31T14:13:38.850 回答
1

您可以尝试numeric_boolean用作类型:

<property column="opener" name="opener" type="numeric_boolean"/>
于 2011-05-31T14:22:01.120 回答
0

这是一个棘手的问题,因为您无权访问数据库。但这可以通过一些工作来完成

您需要做的是创建一个自定义类型类。此类基本上将从数据库中检索值(1 或 0),并将包含返回 true 或 false 布尔对象的逻辑。

这是一个例子:

http://alenovarini.wikidot.com/mapping-a-custom-type-in​​-hibernate

您的新类型映射将如下所示:

    <typedef class="com.path.to.my.package.CustomBooleanType" name="myBoolType" />

该类 nullSafeGet 方法将返回包含 true 或 false 的布尔对象。

所以你的新映射将包含:

<property column="opener" name="opener" type="myBoolType"/>
于 2011-06-03T03:13:29.920 回答