错误的布尔映射Hibernate(ArrayIndexOutOfBoundsException) [英] Erroneous Boolean Mapping Hibernate (ArrayIndexOutOfBoundsException)
问题描述
我拥有一个拥有以下属性的持久化书籍类:
$ b $ ul
我的表格描述如下所示:
到目前为止,一切似乎都很好,但是当我尝试获取表中的所有值时,我会看到以下异常消息:
20:04:43,832 TRACE BasicExtractor:61 - 提取值([classifi1_1_0_]:[BIGINT]) - [11]
20:04:43,832 TRACE BasicExt ractor:61 - 提取值([collecti1_2_1_]:[BIGINT]) - [11]
20:04:43,833 TRACE BasicExtractor:61 - 提取值([book_id1_0_2_]:[BIGINT]) - [1]
20:04:43,839 TRACE BasicExtractor:61 - 提取值([classifi2_1_0_]:[VARCHAR]) - [Prueba]
20:04:43,841 TRACE BasicExtractor:61 - 提取值([collecti2_2_1_]:[VARCHAR ]) - [Prueba]
20:04:43,841 TRACE BasicExtractor:61 - 提取值([book_tit2_0_2_]:[LONGVARCHAR]) - [Libro de Prueba(无验证)]
20:04:43,842 TRACE BasicExtractor:61 - 提取值([book_aut3_0_2_]:[LONGVARCHAR]) - [Jonathan Pichardo]
20:04:43,842 TRACE BasicExtractor:61 - 提取值([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.decodeAndCreateReturnValu e(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等等
我运行的代码是:
Session session = SessionFactoryHandler.buildIfNeeded()。
openSession();
Criteria crit = session.createCriteria(Book.class);
crit.list();
session.close();
SessionFactoryHandler.closeFactory();
据我所知,它发生在状态属性上,不知道为什么,如果我在xml中注释了映射属性,它可以完美地工作,但是对于它来说,它始终会抛出同样的异常,它具有相同的索引57,但它并不影响数据库中该列的值一个注册表)。
映射文件如下所示:
< hibernate-mapping package =com.cetys.librarymanagement>
< class name =com.cetys.librarymanagement.Core.DomainModels.Booktable =book>
< meta attribute =class-description>
此类包含根据ALTAIR系统中的规范书籍的全部描述,
。
< / meta>
< id name =idtype =longcolumn =book_id>
< / id>
< property name =systemIdcolumn =book_system_idtype =longnot-null =true/>
< property name =fullClassificationcolumn =book_full_classification
type =stringnot-null =true/>
<多对一名称=分类列=分类标识
class =com.cetys.librarymanagement.Core.DomainModels.Classificationnot-null =true
unique =falsecascade =save-updatefetch =join/>
class =com.cetys.librarymanagement.Core.DomainModels.Collectionnot-null =false
unique =falsecascade =save-updatefetch =join/>
< / class>
< / hibernate-mapping>
有什么想法?
在MySQL中有一个错误,它会导致数据库中的BIT类型变为布尔类型。 BIT值从版本5.0.3开始,因为它不存储单个BIT值。它存储诸如SET或ENUM之类的东西。当你进行数值比较时,这经常会引发问题。欲了解更多详情,请点击此处
http://www.xaprb.com/blog/2006/04/11/bit-values-in-mysql/
您可以让您的DBA将数据类型更改为tinyint,但如果这不可行,您可以将状态映射从布尔型更改为numeric_boolean,所以会类似于
< property name =statuscolumn =book_statustype =numeric_booleannot-null =true/>
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?
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"/>
这篇关于错误的布尔映射Hibernate(ArrayIndexOutOfBoundsException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!