Question/problem about CHAR mapping done by class ValueStringFixed

516 views
Skip to first unread message

Michel

unread,
Apr 12, 2010, 8:27:53 AM4/12/10
to H2 Database
Hello,
I'm using Hibernate/Spring and I get an
"java.lang.StringIndexOutOfBoundsException: String index out of range:
0" whenever Hibernate is trying to convert back values from DB to
Java.
It's failing on a column of type CHAR wich is handled by
ValueStringFixed.
After taking a closer look at that class, it appears to trim the value
from/to DB.
The problem I have is that I'm trying to insert a single space value
for into CHAR column.
The insert is working but value is set to empty CHAR in DB and
whenever reading it using Hibernate then it gives the
"StringIndexOutOfBoundsException" exception.
So, my question is why is that value trimmed (which prevent it to be a
space) and how to avoid it.
Thanks in advance for any help,

Michel

Thomas Mueller

unread,
Apr 13, 2010, 1:01:43 AM4/13/10
to h2-da...@googlegroups.com
Hi,

Why do you use CHAR and not VARCHAR?

Regards,
Thomas

Michel

unread,
Apr 13, 2010, 9:12:16 AM4/13/10
to H2 Database
I'm using H2 for basic testing and thus it's hibernate which generates
the schema for me.
Unfortunately, the entity definition is done on an existing DB....

Regards,

Michel

Thomas Mueller

unread,
Apr 14, 2010, 1:20:35 PM4/14/10
to h2-da...@googlegroups.com
Hi,

What is your Hibernate configuration, specially what Hibernate
"Dialect" to you use?

> the entity definition is done on an existing DB....

I don't understand, sorry.

What is the complete stack trace (including all root causes)?

Regards,
Thomas

Michel

unread,
Apr 16, 2010, 7:35:00 AM4/16/10
to H2 Database
Hi,
The dialiect used is "org.hibernate.dialect.H2Dialect".
Regards,

Michel
--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To post to this group, send email to h2-da...@googlegroups.com.
To unsubscribe from this group, send email to h2-database...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.

Thomas Mueller

unread,
Apr 19, 2010, 1:14:16 AM4/19/10
to h2-da...@googlegroups.com
Hi,

Normally Hibernate uses VARCHAR and not CHAR for Strings. What is the
complete stack trace (including all root causes)? Could you also post
the complete Hibernate configuration (including mappings and and Java
classes if you configure things with annotations)? What version of
Hibernate do you use?

Michel

unread,
Apr 19, 2010, 3:23:04 AM4/19/10
to H2 Database
Hi,
I use annotation and the Java type of the field is "Character" which
is handled by the "ValueStringFixed" type of H2.
The complete stack trace is:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 0
at java.lang.String.charAt(String.java:558)
at org.hibernate.type.CharacterType.get(CharacterType.java:52)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:184)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:173)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:105)
at
org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:
2114)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1404)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1332)
at org.hibernate.loader.Loader.getRow(Loader.java:1230)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:603)
at org.hibernate.loader.Loader.doQuery(Loader.java:724)
at
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:
259)
at org.hibernate.loader.Loader.doList(Loader.java:2232)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
at org.hibernate.loader.Loader.list(Loader.java:2124)
at
org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:
118)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1597)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
at
be.ucm.cas.signaletique.vm.dao.hibernate.InarecHibernateDAO.getInarecWithNiss(InarecHibernateDAO.java:
43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:585)
at
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:
307)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:
182)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:
149)
at
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:
106)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:
171)
at
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:
204)
at $Proxy54.getInarecWithNiss(Unknown Source)
at
be.ucm.cas.preaffiliation.ti.service.impl.PreAffiliationServiceImpl.getInarecWithNiss(PreAffiliationServiceImpl.java:
848)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:585)
at
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:
307)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:
182)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:
149)
at
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:
106)
at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:
171)
at
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:
204)
at $Proxy71.getInarecWithNiss(Unknown Source)
at
be.ucm.cas.preaffiliation.societe.batch.AffiliationSociete.launch(AffiliationSociete.java:
125)
at
be.ucm.cas.preaffiliation.societe.batch.AffiliationSociete.main(AffiliationSociete.java:
1172)
at
be.ucm.cas.preaffiliation.societe.batch.AffiliationSociete.main(AffiliationSociete.java:
1145)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:
110)

The class containing the problematic mapping is Inarec and the mapping
itself is

private Character trt; // Pour savoir qui traite le flux
@Column(name="TRT")
public Character getTrt() {
return trt;
}

Hibernate config is:
<bean
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
id="sessionFactory">
<property name="dataSource" ref="datasource"/>
<property name="entityInterceptor">
<ref bean="myTstInterceptor"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.cache.use_query_cache">true</
prop>
<prop
key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</
prop>
<prop
key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop
key="hibernate.jdbc.use_streams_for_binary">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>e.ucm.cas.signaletique.vm.model.Inarec</value>
</list>
</property>
</bean>

With following data source config:
<bean name="datasource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:/Projects/DB/h2db/dbSrv"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>

The problem only occurs whenever the value of the TRT column is " " (a
single space) because it's trimmed by the "ValueStringFixed.get(String
s)" method first and then the first character is taken.......
The Hibernate version is "3.3.2-GA".

Thanks for your help,

Michel

Thomas Mueller

unread,
Apr 22, 2010, 3:17:11 PM4/22/10
to h2-da...@googlegroups.com
Hi,

I understand now (it took me a long time...). This sounds like an
incompatibility between the behavior of H2 and Hibernate. I'm not sure
how to best fix it. Currently the only workaround I know is not to use
Character, sorry.

Thomas Mueller

unread,
Jun 28, 2010, 2:38:03 PM6/28/10
to h2-da...@googlegroups.com
Hi,

According to my test case, H2 behaves like PostgreSQL and MySQL
(trailing spaces are trimmed). I added a feature request
"Compatibility for data type CHAR (Derby, HSQLDB).". See also
http://code.google.com/p/h2database/issues/detail?id=212

Regards,
Thomas

Thomas Mueller

unread,
Oct 15, 2012, 3:36:10 PM10/15/12
to h2-da...@googlegroups.com
Hi,

It seems our are using an Informix database, so I suggest to seek more info from Informix support and not H2 support.

Regards,
Thomas


On Wed, Sep 26, 2012 at 4:05 PM, Charan Kurella <charan....@gmail.com> wrote:
I have got same problem on Informix database and I am using

<property name="hibernate.connection.url" value="jdbc:informix-sqli://url:informixserver=xxx;user=xxx;password=xxx;IFX_LOCK_MODE_WAIT=1;IFX_TRIMTRAILINGSPACES=1"/>

It is causing

Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:695)
at org.hibernate.type.CharacterType.get(CharacterType.java:29)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2096)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1380)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308)
at org.hibernate.loader.Loader.getRow(Loader.java:1206)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)

Can someone suggest how to getaway with this

Thanks
--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To view this discussion on the web visit https://groups.google.com/d/msg/h2-database/-/GeEgVCTZdxUJ.

Charan Kurella

unread,
Oct 23, 2012, 5:28:34 AM10/23/12
to h2-da...@googlegroups.com
Yes, we are using informix database. I thought it is the similar problem we found with Hibernate and I want to know what you did to fix the issue in H2.

Thank you
Reply all
Reply to author
Forward
0 new messages