Weblog

Ibatis, mapping Calendar to date

We use the genValueTypes ant-task from Oracle to generate our pojo model.

<oracle :genValueTypes schema="@{schema}" debug="true"
output="${srcgen.dir}"/>

If I create some elements of type date in my xsd and let the task generate my pojos it will generate attributes of type Calendar for me.

protected java.util.Calendar startDatum;

I’m using the next ibatis config to call a stored procedure with an inputvariable of type date :

<resultmap id="result5"
class="nl.iteye.model.Employee">
<result property="startDatum" column=" startDatum"/>
</resultmap>
<parametermap id="parameters5" class="map">
<parameter property="startDatumBegin" jdbcType="DATE"
javaType="java.util.Date" mode="IN"/>
<parameter property=" startDatumEind" jdbcType="DATE"
javaType="java.util.Date" mode="IN"/>
<parameter property="refIdBegin" jdbcType="NUMERIC"
javaType="java.lang.Integer" mode="IN"/>
<parameter property=" refIdEind" jdbcType="NUMERIC"
javaType="java.lang.Integer" mode="IN"/>
<parameter property="employees" javaType="java.sql.ResultSet"
jdbcType="ORACLECURSOR" mode="OUT" resultMap="result5"/>
<parameter property="resultaat" jdbcType="STRUCT" typeName="T_RESULTAAT"
javaType="nl.iteye.model.algemeen.ServiceResult"
mode="OUT"
typeHandler="nl.iteye.model.algemeen.typehandler.AlgemeenHandlerCallback"/>
</parametermap>
<procedure id="listEmployees" parameterMap="parameters5">
< ![CDATA[
{ call mypackage.getEmployeeList(?,?,?,?,?,?) }
]]>
</procedure>

Ibatis code :

SqlMapClient sqlMap = CustomSqlMapClient.getSqlMap(CustomSqlMapClient.LV_RESOURCE);

Map pars = new HashMap();
pars.put("startDatumBegin", startDatumBegin);
pars.put("startDatumEind", startDatumEind);
pars.put("refIdBegin", refIdBegin);
pars.put("refIdBegin", refIdBegin);

sqlMap.queryForObject("listEmployees", pars);

Ibatis won’t be able to map the startDatum from my resultMap to the startDatum of my pojo. The next error will be generated


--- Cause: com.ibatis.sqlmap.client.SqlMapException: No type handler could be found to map the property 'startDatum' to the column 'startDatum'.  One or both of the types, or the combination of types is not supported.

You could write your own serializer and i assume there will be several other solutions available. The solution I picked was to write a Calendar typehandler for ibatis.

public class CalendarTypeHandlerCallback implements TypeHandlerCallback {

public Object getResult(ResultGetter getter) throws SQLException {
Date date = getter.getDate();
Calendar calendar = null;

if (date != null) {
calendar = Calendar.getInstance();
calendar.setTime(date);
}

return calendar;
}

public void setParameter(ParameterSetter setter, Object parameter)
throws SQLException {
GregorianCalendar calendar = (GregorianCalendar) parameter;
Date date = new Date(calendar.getTimeInMillis());
setter.setDate(date);
}

public Object valueOf(String s) {
return s;
}
}

Just an easy conversion of the date to calendar.

The resultMap will now look like :

<resultmap id="result5"
class="nl.iteye.model.Employee">
<result property="startDatum" column=" startDatum"
javaType="java.util.Calendar" jdbcType="DATE" typeHandler="nl.iteye.model.algemeen.typehandler.CalendarTypeHandlerCallback"/>
</resultmap>

After running the code, ibatis will be able to do the mapping the between the date and the calendar.

Share and Enjoy:
  • del.icio.us
  • Google Bookmarks
  • DZone
  • LinkedIn
  • SphereIt
  • StumbleUpon
  • Technorati

2 Responses to “Ibatis, mapping Calendar to date”

  1. Sarath Says:

    One small suggestion make the CalendarTypeHandlerCallback truly potable. To be able to run this sample on different VMs and still maintain Time Correctness, Always save and retrieve using some networkCalendar. That way the true (long milsecs) value of the date will not change if running on VMs in Different TimeZone. I explain this in much detail: http://blog.sarathonline.com/2009/01/java-timezone-mapping-calendar-to.html

  2. Eric Elzinga Says:

    Thanks for the info!

Leave a Reply

Technology