IBatis ResultMaps: column name <...> not found in this ResultSet

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

But this is a little annoying if you want to reuse ResultMaps, especially when you have "resultmaps in resultsmaps".

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 request that uses a connection to load container objects containing myPropName, and someCollection contains a collection of internal objects.

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

Column name "otherColumnName" was not found in this ResultSet

Is there no way to initialize someCollection with a null or empty collection if the corresponding properties (in this case otherPropName) are not present in the SQL query?

Is it really necessary to create another scorecard for this scenario?

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

+4
source share
1 answer

"Problem" in all implementations of TypeHandler . These objects must retrieve the column value from the ResultSet , matching it with the corresponding java type. For example, in the StringTypeHandler class StringTypeHandler there is such a way:

 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 in the ResultSet , the rs.getString(columnName) row throws a SQLException . The only way to avoid this error is to write your own TypeHandler , which returns null instead of throwing an exception.
Anyway, I suggest you use two result files.

+6
source

Source: https://habr.com/ru/post/1347158/


All Articles