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…
The “problem” is in all the
TypeHandlerimplementations. These objects have to extract the column value fromResultSetmapping it to the corresponding java type. For instance, inStringTypeHandlerclass there’s a method like this:If the column is not present in the
ResultSet, the liners.getString(columnName)throws aSQLException. The only way to avoid this error is writing your ownTypeHandlerwhich returnsnullinstead of throwing the exception.Anyhow, I suggest you to use two resultmaps.