Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iBatis ResultMaps: The column name <...> was not found in this ResultSet

Given a ResultMap for an iBatis select query, it seems obligatory that all columns (that are mapped to properties in the ResultMap) are actually part of the SQL query.

But that's a bit annoying if one wants to reuse ResultMaps, particularly when having 'resultmaps within resultmaps'.

Example:

    <resultMap id="myResultMap"
      <result property="myPropName" column="myColumnName"/>
      <result property="someCollection" resultMap="otherResultMap"/>
    </resultMap>

    <resultMap id="otherResultMap" groupBy="..."
      <result property="otherPropName" column="otherColumnName"/>
    </resultMap>

Of course, these two result maps are defined because there is a case with a query that uses a join to load the container objects holding myPropName and someCollection holding a collection of inner objects.

But if I want to reuse the same result map definition for another select query that only needs to load the container objects (with myPropName), but doesn't need to load the inner objects (into someCollection), then there will be an error message:

The column name 'otherColumnName' was not found in this ResultSet

Is there no possibility that allows to initialize the someCollection with null or an empty collection if the respective properties (in this case otherPropName) are not present in the SQL query?

Is it really necessary to create another result map altogether for that scencario?

Using the iBatis (not myBatis yet) version 2.3.4...

like image 891
Markus Pscheidt Avatar asked Apr 07 '11 19:04

Markus Pscheidt


1 Answers

The "problem" is in all the TypeHandler implementations. These objects have to extract the column value from ResultSet mapping it to the corresponding java type. For instance, in StringTypeHandler class there's a method like this:

public Object getResult(ResultSet rs, String columnName) throws SQLException {
    Object s = rs.getString(columnName);
    if (rs.wasNull()) {
        return null;
    } else {
        return s;
    }
}

If the column is not present in the ResultSet, the line rs.getString(columnName) throws a SQLException. The only way to avoid this error is writing your own TypeHandler which returns null instead of throwing the exception.
Anyhow, I suggest you to use two resultmaps.

like image 112
javanna Avatar answered Sep 24 '22 19:09

javanna