Erroneous Boolean Mapping Hibernate (ArrayIndexOutOfBoundsException)

故事扮演 提交于 2019-12-18 09:26:44

问题


I have a persistend Book Class with the following properties

  • PropertyName -> HibernateMappingType -> JavaType
  • id -> long -> long
  • title -> text -> String
  • author -> string -> String
  • systemId -> long -> long
  • status -> boolean -> boolean
  • fullClassification -> string -> string

And my table description looks like this:

So far everything seems good, but when I try to fetch all the values in the table I get the following Exception Message:

   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)

etc etc etc

The code I'm running is:

Session session = SessionFactoryHandler.buildIfNeeded(). openSession();

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

    crit.list();

    session.close();

    SessionFactoryHandler.closeFactory();

As I understand it is happening on the status property I just don't know why, if I comment the mapping property in the xml it works perfectly but with it it throws always the same exception with the same index 57, it doesn't make a difference the value of that column in the database (which has only one registry).

The mapping file is like follows:

<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>

Any ideas?


回答1:


From what I see you are trying to map the BIT type in Database to Boolean in your hibernate code.

There is a bug in MySQL with BIT Value, from version 5.0.3 onwards, in that it does not store a single BIT value. It stores something like SET or ENUM. And that often throws up issues, when you are doing a numeric value comparison. For more details check here

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

You could ask your DBA to change the datatype to tinyint, however if that is not possible, you can change the status mapping from boolean to numeric_boolean, so would be something like

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



回答2:


There are two ways you can achieve the type conversion of attribute

By annotating field with Type

@Type(type = "yes_no")
private boolean isActive;

in DB Y/N will get persisted.

By writing a converter

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

Converter class

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;
    }

}

in xml you can replace the type attribute with the converter class name or yes_no

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


来源:https://stackoverflow.com/questions/37914191/erroneous-boolean-mapping-hibernate-arrayindexoutofboundsexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!