Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iBatis not populating object when there are no rows found

I am running a stored procedure that returns 2 cursors and none of them have any data. I have the following mapping xml:

<resultMap id="resultMap1" class="HashMap">
  <result property="firstName" columnIndex="2"/>
</resultMap>

<resultMap id="resultMap2" class="com.somePackage.MyBean">
  <result property="unitStreetName" column="street_name"/>
</resultMap>

<parameterMap id="parmmap" class="map">
  <parameter property="id" jdbcType="String" javaType="java.lang.String" mode="IN"/>
  <parameter property="Result0" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet" mode="OUT" resultMap="resultMap1"/>
  <parameter property="Result1" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet" mode="OUT" resultMap="resultMap2"/>
</parameterMap>

<procedure id="proc" parameterMap="parmmap">
    { call my_sp (?,?,?) }
</procedure>

First result set is being put in a HashMap...second resultSet is being put in a MyBean class.

code in my DAO follows:

HashMap map = new HashMap()
map.put("id", "1234");
getSqlMapClientTemplate().queryForList("mymap.proc", map);
HashMap result1 = (HashMap)((List)parmMap.get("Result0")).get(0);
MyBean myObject = (MyBean)((List)parmMap.get("Result1")).get(0);//code fails here

in the last line above..my code fails. It fails because second cursor has no rows and thats why nothing is put into the list. However, first cursor returns nothing as well but since results are being put into a HashMap the list for first cursor atleast has HashMap object inside it..

Why this difference? is there a way to make iBatis put an object of MyBean inside the list even if there are no rows returned? Or should I be handling this in my DAO...I want to avoid handling it in the DAO because I have whole bunch of DAO's like these.

like image 995
Omnipresent Avatar asked Nov 10 '09 19:11

Omnipresent


1 Answers

Ibatis does not instantiate or return objects if the resultset is empty (or in your case, if the third parameter of your stored procedure returns null).

I noticed when writing typeHandlers that they don't even get called when no result is returned, so that route will also not help.

I'm sure you have good reasons to instantiate empty objects in this case, but I'm afraid the only way is to detect nulls in your DAO.

If you have lots of DAO's with this problem, you could have them extend a superclass, and have a convenience method there which checks for an empty list or null object and return an empty object in that case.

like image 181
Rolf Avatar answered Sep 30 '22 12:09

Rolf