Sorry for the long post, I have a couple of questions that need some background.
I have several columns in a MySql table that are defined as char(1). Some rows in this column contain an empty value. It is not null, but empty. When reading this in using Hibernate, it throws an exception:
Code:
55379 [main] INFO org.hibernate.type.CharacterType - could not read column value from result set: col_2_0_; String index out of range: 0
[ERROR] Error: String index out of range: 0
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:686)
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.loader.hql.QueryLoader.getResultColumnOrRow(QueryLoader.java:361)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:629)
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.hql.QueryLoader.list(QueryLoader.java:401)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.usgs.itis.itis_load.TwbImport.importTU(TwbImport.java:340)
at org.usgs.itis.itis_load.TwbImport.main(TwbImport.java:734)
After some creative searching here, I saw this topic:
character can be empty in mysql, but it seemed to be unresolved.
Looking at CharacterType.java, the get method is defined as:
Code:
public Object get(ResultSet rs, String name) throws SQLException {
String str = rs.getString(name);
if (str==null) {
return null;
}
else {
return new Character( str.charAt(0) );
}
}
This is in the 3.3.2_GA code and still in trunk.
So you can see if the string from the database is not null, but is zero lengh, it will throw the array out of bounds exception. It seems this issue could be eliminated by changing the if to
if (str == null || str.length() == 0), however I don't know what other repercussions that would have.
I created a class EmptyCharacterType that extends CharacterType and overrode the get() method using the if statement from above to fix this in my code. Then I changed my .hbm.xml file to replace java.lang.Character with myPackage.EmptyCharacterType. This works for now and I am investigating why the char(1) columns have the odd state.
So, here are my questions.
1. Would the change to the if in CharacterType be a bad thing?
2. Is there a better way of solving this (as opposed to writing a custom CharacterType) if I can't get the MySql columns straightened out?
Many thanks,
alan hampson