ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexander Garbuz <alexander.gar...@isthmusgroup.com>
Subject A problem with custom data handler while trying to execute the insert statement
Date Mon, 11 Apr 2005 15:39:01 GMT
Hi all,

I am having a problem with custom data handler while trying to execute 
the insert statement.

I need to convert java.util.GregorianCalendar to a DATE field in MySQL 
database. When I am trying to insert a new row into a table I am getting 
a NullPointerException. It works perfectly when I need to fetch a row 
from a table but fails when I need to insert it.

I tried to debug the code and found out that the data handler value for 
the "assignmentDate" is set to null.


Here is the error message I get:

Mon Apr 11 10:02:03 CDT 2005-ERROR[SQLException in the create method of 
AssignmentDao [com.ibatis.common.jdbc.exception.NestedSQLException:  
--- The error occurred in config/sqlmap_mysql/Assignment.xml. 
--- The error occurred while applying a parameter map. 
--- Check the create-InlineParameterMap. 
--- Check the parameter mapping for the 'assignmentDate' property. 
--- Cause: java.lang.NullPointerException
Caused by: 
java.lang.NullPointerException]com.ibatis.common.jdbc.exception.NestedSQLException:   

--- The error occurred in config/sqlmap_mysql/Assignment.xml. 
--- The error occurred while applying a parameter map. 
--- Check the create-InlineParameterMap. 
--- Check the parameter mapping for the 'assignmentDate' property. 
--- Cause: java.lang.NullPointerException
Caused by: java.lang.NullPointerException]

Here is the code that I am using:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap
    PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
    "http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap>
    <typeAlias alias="assignmentBo"  
type="com.gss.claims.busobj.AssignmentBo"/>
    <typeAlias alias="assignmentKey" 
type="com.gss.claims.busobj.AssignmentKey"/>

  <resultMap id="assignmentBoResult" class="assignmentBo">
    <result property="assignmentId" column="assignment_id"/>
    <result property="assignmentTypeId" column="assignment_type_id"    
javaType="java.lang.Integer" jdbcType="NUMERIC"/>
    <result property="assignmentDate"
            column="assignment_date" 
            javaType="java.util.GregorianCalendar" 
            jdbcType="DATE"
            
typeHandler="com.gss.claims.dao.GregorianCalendarTypeHandlerCallback" />
  </resultMap>
  <insert id="create"
          parameterClass="assignmentBo">
    INSERT INTO assignment (           
        assignment_id,       
        assignment_type_id,              
        assignment_date          
    ) VALUES (           
        #assignmentId#,
        #assignmentTypeId#,
        #assignmentDate#
    )
  </insert>
  <select id="findByPrimaryKey"
          resultClass="assignmentBo"
          resultMap="assignmentBoResult"
          parameterClass="java.lang.Integer">
    SELECT * FROM assignment WHERE assignment_id=#value#
  </select>
</sqlMap>

here is the type handler class

package com.gss.claims.dao;

import java.sql.Date;
import java.sql.SQLException;
import java.util.GregorianCalendar;

import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;
/**
 * Performs processing on a value before it is used to set
 * the parameter of a PreparedStatement.
 *
 * @param setter    The interface for setting the value on the 
PreparedStatement.
 * @param parameter The value to be set.
 * @throws SQLException If any error occurs.
 */
public class GregorianCalendarTypeHandlerCallback implements 
TypeHandlerCallback {
      /**
       * Performs processing on a value before it is used to set
       * the parameter of a PreparedStatement.
       *
       * @param setter    The interface for setting the value on the 
PreparedStatement.
       * @param parameter The value to be set.
       * @throws SQLException If any error occurs.
       */
    public Object getResult(ResultGetter getter)
    throws SQLException {

        Date date = getter.getDate();
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        return calendar;
    }
      /**
       * Performs processing on a value before after it has been retrieved
       * from a ResultSet.
       *
       * @param getter The interface for getting the value from the 
ResultSet.
       * @return The processed value.
       * @throws SQLException If any error occurs.
       */
    public void setParameter(ParameterSetter setter, Object parameter)
    throws SQLException {
        GregorianCalendar calendar = (GregorianCalendar)parameter;
        Date date = new Date(calendar.getTimeInMillis());
        setter.setDate(date);
    }
      /**
       * Casts the string representation of a value into a type 
recognized by
       * this type handler.  This method is used to translate nullValue 
values
       * into types that can be appropriately compared.  If your custom 
type handler
       * cannot support nullValues, or if there is no reasonable string 
representation
       * for this type (e.g. File type), you can simply return the 
String representation
       * as it was passed in.  It is not recommended to return null, 
unless null was passed
       * in.
       *
       * @param s A string representation of a valid value for this type.
       * @return One of the following:
       *         <ol>
       *         <li>the casted repersentation of the String value,</li>
       *         <li>the string as is,</li>
       *         <li>null, only if null was passed in.</li>
       *         </ol>
       */     
    public Object valueOf(String s){
        return s;
    }
}


It looks like this portion of the map 
(typeHandler="com.gss.claims.dao.GregorianCalendarTypeHandlerCallback") 
is not being set. Any ideas are very appreciated?

Thanks.

-- 
Alexander Garbuz

Isthmus Group Inc.
222 State Street
Suite 300
Madison, WI  53703
phone: (608) 661-1234
cell:  (608) 628 2448
alexander.garbuz@isthmusgroup.com
www.isthmusgroup.com 


Mime
View raw message