错误的布尔映射Hibernate(ArrayIndexOutOfBoundsException)

时间:2016-06-20 03:21:03

标签: java hibernate boolean indexoutofboundsexception

我有一个具有以下属性的持久Book类

  • PropertyName - > HibernateMappingType - > java类型
  • id - >长 - >长
  • title - >文字 - >串
  • 作者 - > string - >串
  • systemId - >长 - >长
  • 状态 - >布尔值 - >布尔
  • fullClassification - > string - >串

我的表格描述如下: Book Table Diagram

到目前为止一切似乎都很好,但是当我尝试获取表中的所有值时,我得到以下异常消息:

   20:04:43,832 TRACE BasicExtractor:61 - extracted value ([classifi1_1_0_] : [BIGINT]) - [11]
20:04:43,832 TRACE BasicExtractor:61 - extracted value ([collecti1_2_1_] : [BIGINT]) - [11]
20:04:43,833 TRACE BasicExtractor:61 - extracted value ([book_id1_0_2_] : [BIGINT]) - [1]
20:04:43,839 TRACE BasicExtractor:61 - extracted value ([classifi2_1_0_] : [VARCHAR]) - [Prueba]
20:04:43,841 TRACE BasicExtractor:61 - extracted value ([collecti2_2_1_] : [VARCHAR]) - [Prueba]
20:04:43,841 TRACE BasicExtractor:61 - extracted value ([book_tit2_0_2_] : [LONGVARCHAR]) - [Libro de Prueba (No Existe) ]
20:04:43,842 TRACE BasicExtractor:61 - extracted value ([book_aut3_0_2_] : [LONGVARCHAR]) - [Jonathan Pichardo]
20:04:43,842 TRACE BasicExtractor:61 - extracted value ([book_sys4_0_2_] : [BIGINT]) - [190996]
java.lang.ArrayIndexOutOfBoundsException: 57
    at com.mysql.cj.mysqla.MysqlaUtils.bitToLong(MysqlaUtils.java:68)
    at com.mysql.cj.core.io.MysqlTextValueDecoder.decodeBit(MysqlTextValueDecoder.java:231)
    at com.mysql.cj.jdbc.ResultSetRow.decodeAndCreateReturnValue(ResultSetRow.java:170)
    at com.mysql.cj.jdbc.ResultSetRow.getValueFromBytes(ResultSetRow.java:269)
    at com.mysql.cj.jdbc.BufferRow.getValue(BufferRow.java:349)
    at com.mysql.cj.jdbc.ResultSetImpl.getNonStringValueFromRow(ResultSetImpl.java:813)
    at com.mysql.cj.jdbc.ResultSetImpl.getBoolean(ResultSetImpl.java:904)
    at com.mysql.cj.jdbc.ResultSetImpl.getBoolean(ResultSetImpl.java:908)
    at org.hibernate.type.descriptor.sql.BooleanTypeDescriptor$2.doExtract(BooleanTypeDescriptor.java:59)
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
等等等

我正在运行的代码是:

会话会话= SessionFactoryHandler.buildIfNeeded()。                 的openSession();

    Criteria crit = session.createCriteria( Book.class );

    crit.list();

    session.close();

    SessionFactoryHandler.closeFactory();

据我所知,它发生在 status 属性上我只是不知道为什么,如果我在xml中注释map属性它完美地运行但是它会抛出相同的异常相同的索引57,它没有区别数据库中该列的值(只有一个注册表)。

映射文件如下:

<hibernate-mapping package="com.cetys.librarymanagement">
    <class name="com.cetys.librarymanagement.Core.DomainModels.Book" table="book">
        <meta attribute="class-description">
            This class contains the whole description of a Book,
            according to the specification in ALTAIR system.
        </meta>
        <id name="id" type="long" column="book_id">
        </id>
        <property name="title" column="book_title" type="text" length="500" not-null="true"/>
        <property name="author" column="book_author" type="text" not-null="true"/>
        <property name="systemId" column="book_system_id" type="long" not-null="true"/>
        <property name="status" column="book_status" type="boolean" not-null="true"/>
        <property name="fullClassification" column="book_full_classification" 
                  type="string" not-null="true"/>

        <many-to-one name="classification" column="classification_id" 
                     class="com.cetys.librarymanagement.Core.DomainModels.Classification" not-null="true" 
                     unique="false" cascade="save-update" fetch="join"/>
        <many-to-one name="collection" column="collection_id"
                     class="com.cetys.librarymanagement.Core.DomainModels.Collection" not-null="false" 
                     unique="false" cascade="save-update" fetch="join"/>
    </class>
</hibernate-mapping>

有什么想法吗?

2 个答案:

答案 0 :(得分:5)

从我看到你试图将数据库中的BIT类型映射到你的休眠代码中的布尔值。

从版本5.0.3开始,MySQL中存在BIT值的错误,因为它不存储单个BIT值。它存储类似SET或ENUM的东西。当您进行数值比较时,这通常会引发问题。有关详细信息,请点击此处

http://www.xaprb.com/blog/2006/04/11/bit-values-in-mysql/

您可以要求您的DBA将数据类型更改为tinyint,但是如果不可能,您可以将状态映射从boolean更改为numeric_boolean,这样就像

<property name="status" column="book_status" type="numeric_boolean" not-null="true"/>

答案 1 :(得分:3)

有两种方法可以实现属性

的类型转换

使用Type

注释字段
@Type(type = "yes_no")
private boolean isActive;
数据库Y / N中的

将保持不变。

通过编写转换器

@Column
@Convert(converter = BooleanConverter.class)
private boolean isActive;

转换器类

public class BooleanConverter implements AttributeConverter<Boolean, Character> {

    @Override
    public Character convertToDatabaseColumn(Boolean attribute) {
        if (attribute)
            return 'Y';
        else
            return 'N';
    }

    @Override
    public Boolean convertToEntityAttribute(Character dbData) {
        if ('Y' == dbData)
            return true;
        else
            return false;
    }

}
<\ n>在xml中,您可以使用转换器类名称替换type属性或yes_no

<property name="status" column="book_status" type="yes_no" not-null="true"/>