1

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: Book Table Diagram

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?

J. Pichardo
  • 3,077
  • 21
  • 37

2 Answers2

5

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"/>
  • Thanks for the info. It works perfectly with tinyint, but if I add the numeric_boolean type and leave the column as bit it doesn't work. – J. Pichardo Jun 20 '16 at 03:43
  • This solution saved my day for another problem :) Apache Hive has been giving exactly same error when i run it with debug mode. According to this solution I changed the init script of metastoredb and it worked. What I changed in hive-schema-1.2.0.mysql.sql as follows; `DEFERRED_REBUILD` bit(1) NOT NULL, `IS_COMPRESSED` bit(1) NOT NULL, `IS_STOREDASSUBDIRECTORIES` bit(1) NOT NULL, to `DEFERRED_REBUILD` TINYINT(1) NOT NULL, `IS_COMPRESSED` TINYINT(1) NOT NULL, `IS_STOREDASSUBDIRECTORIES` TINYINT(1) NOT NULL, – Vezir Nov 14 '17 at 12:02
3

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"/>
Saravana
  • 12,647
  • 2
  • 39
  • 57
  • I like this approach thanks, in fact it gave me several ideas on how to solve other issues. – J. Pichardo Jun 20 '16 at 03:44
  • @Saravana this wouldn't work without @Type("yes_no") annotation in the entity? If want to use just xml? – pinkpanther Aug 02 '16 at 11:57
  • 1
    If you are based on annotation you can use `Convert` on field/getter, if based on `xml` use `type` attribute, don't mix both the approches – Saravana Aug 02 '16 at 11:59
  • 1
    "in xml you can replace the type attribute with the converter class name" this doesn't seem to be the case, we are getting `hibernate could not determine type for` error. – Andy Hayden Jul 22 '19 at 22:29