Mysql – Hibernate, Null values and nativeSQL

hibernateMySQL

I'm using hibernate with mySQL and I've the following problem: I do a nativeSQL query on a table with, among others, a column that is a character long. This column can get to null. Now I receive the following exception while querying:

4-feb-2010 10.31.00 org.hibernate.type.NullableType nullSafeGet
INFO: could not read column value from result set: nazeuro; 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:29)
        at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
        at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:189)
        at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.extract(CustomLoader.java:474)
        at org.hibernate.loader.custom.CustomLoader$ResultRowProcessor.buildResultRow(CustomLoader.java:420)
        at org.hibernate.loader.custom.CustomLoader.getResultColumnOrRow(CustomLoader.java:317)
        at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:606)
        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.custom.CustomLoader.list(CustomLoader.java:289)
        at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
        at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
        at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
        at Views.Components.QuickLookup$updater.run(QuickLookup.java:102)

I've been googling to solve the issue, I even found posts here on SO, but everytime it is about mapping to objects and writing custom classes to handle the issue. In this situation what I need is a Object array. I know a solution could be to change the table but I'd avoid it as I've to work with a pre-existing db. So do you have any possible solutions?


SQLQuery query = sess.createSQLQuery("SELECT * from " + table + " WHERE " + filter + " LIKE '" + search + "%'");
                    System.out.println(query.toString());
                    List<Object[]> result = query.list();

Best Answer

Which Hibernate version are you using? In 3.3.2, this is CharacterType.get():

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

In case this wasn't obvious, the exception indicates that you got an empty, non-null string ("") from the result set, which causes CharacterType to barf, as this is not an expected value for a CHAR(1). (This is a CHAR(1) column, is it?)

Related Topic