簡體   English   中英

用 hibernate 映射 boolean

[英]Mapping a boolean with hibernate

我遇到了 hibernate 的麻煩。 我最近將我的 hbm2ddl 設置為驗證,它一直在抱怨錯誤的數據類型。 除了布爾值,我已經解決了所有問題。

我的 class 中有一個字段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"/>

並創建您的 class:

import org.hibernate.usertype.UserType;

public class YourClassUserType implements UserType{
 ...
}

您必須從接口 UserType 實現方法。 該實現會將字節轉換為 boolean(因為 TINYINT 在 Java 中映射為字節)

在此處查看示例

祝你好運:)

對於遇到與我相同的麻煩的任何人,我使用了此處發布的兩個答案的組合。

我實現了一個自定義用戶類型來處理我的 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它就像一個魅力:)

您可以將 DB 列定義為char(1) ,並在 Hibernate 映射文件中將屬性定義為type="yes_no" ,即 Java Z27226C864BAC7454A8504F8EDB15D5 類型。 這些將在 DB 中顯示為YN值。

如果你想使用tiny_int ,那么大小必須為 1,但我不能 100% 確定它是如何映射到 HBM 文件中的。

嘗試這個:

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

假設你有一個吸氣劑

 boolean isOpener() ;

和一個二傳手

void setOpener(boolean b);

您可以嘗試使用numeric_boolean作為類型:

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

這是一個棘手的問題,因為您無權訪問數據庫。 但這可以通過一些工作來完成

您需要做的是創建一個自定義類型 class。 這個 class 將基本上從數據庫中檢索值(1 或 0),它將包含返回真或假 Boolean object 的邏輯。

這是一個例子:

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

您的新類型映射將如下所示:

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

該類 nullSafeGet 方法將返回包含真或假的 Boolean object。

所以你的新映射將包含:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM