---
title: "非原生数据类型列值为null时的处理"
author: "xj84"
date: "2011-09-06T17:20:04Z"
tags: []
categories: ["编程语言"]
slug: "null"
draft: false
lastmod: "2011-09-06T17:20:04Z"
id: 1166867
url: "https://xj84.iteye.com/blog/1166867"
---

今天在部署应用的时候发现如下错误：

 

org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL \[]; SQL state \[null];

 error code \[0];   

--- The error occurred in META-INF/ibatis/sqlmap/PT\_JOB\_REC.xml.  

--- The error occurred while applying a result map.  

--- Check the PT\_JOB\_REC.jobRecResult.  

--- The error happened while setting a property on the result object.  

--- Cause: java.lang.RuntimeException: Error setting property 'setSiteId' of 'com.alibaba.dw.powertrace.common.zephyr.common.JobRec@

1888da5'.  Cause: java.lang.IllegalArgumentException; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:   

--- The error occurred in META-INF/ibatis/sqlmap/PT\_JOB\_REC.xml.  

--- The error occurred while applying a result map.  

--- Check the PT\_JOB\_REC.jobRecResult.  

--- The error happened while setting a property on the result object.  

--- Cause: java.lang.RuntimeException: Error setting property 'setSiteId' of 'com.alibaba.dw.powertrace.common.zephyr.common.JobRec@

1888da5'.  Cause: java.lang.IllegalArgumentException

        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.

java:83)

        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.

java:80)

        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.

java:80)

        at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:212)

        at org.springframework.orm.ibatis.SqlMapClientTemplate.executeWithListResult(SqlMapClientTemplate.java:249)

        at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForList(SqlMapClientTemplate.java:296)

        at com.alibaba.dw.powertrace.common.dao.impl.AbstractBaseDaoImpl.getList(AbstractBaseDaoImpl.java:127)

        at com.alibaba.dw.powertrace.common.zephyr.dao.impl.ZephyrDaoImpl.getJobListByJobStatus(ZephyrDaoImpl.java:173)

        at com.alibaba.dw.powertrace.common.zephyr.service.impl.ZephyrServiceImpl.getJobListByJobStatus(ZephyrServiceImpl.java:70)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

        at java.lang.reflect.Method.invoke(Method.java:597)

        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)

        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)

        at $Proxy70.getJobListByJobStatus(Unknown Source)

        at com.alibaba.dw.powertrace.common.zephyr.master.ZephyrJobService.recoverJob(ZephyrJobService.java:57)

        at com.alibaba.dw.powertrace.common.zephyr.master.ZephyrJobService.run(ZephyrJobService.java:108)

        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)

        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)

        at java.lang.Thread.run(Thread.java:662)

Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:   

--- The error occurred in META-INF/ibatis/sqlmap/PT\_JOB\_REC.xml.  

--- The error occurred while applying a result map.  

--- Check the PT\_JOB\_REC.jobRecResult.  

--- The error happened while setting a property on the result object.  

--- Cause: java.lang.RuntimeException: Error setting property 'setSiteId' of 'com.alibaba.dw.powertrace.common.zephyr.common.JobRec@

1888da5'.  Cause: java.lang.IllegalArgumentException

        at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:204)

        at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryForList(MappedStatement.java:139)

        at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:567)

        at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:541)

        at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:118)

        at org.springframework.orm.ibatis.SqlMapClientTemplate$3.doInSqlMapClient(SqlMapClientTemplate.java:298)

        at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:209)

        ... 17 more

Caused by: java.lang.RuntimeException: Error setting property 'setSiteId' of 'com.alibaba.dw.powertrace.common.zephyr.common.JobRec@

1888da5'.  Cause: java.lang.IllegalArgumentException

        at com.ibatis.sqlmap.engine.accessplan.PropertyAccessPlan.setProperties(PropertyAccessPlan.java:52)

        at com.ibatis.sqlmap.engine.exchange.JavaBeanDataExchange.setData(JavaBeanDataExchange.java:112)

        at com.ibatis.sqlmap.engine.mapping.result.ResultMap.setResultObjectValues(ResultMap.java:371)

        at com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback.handleResultObject(RowHandlerCallback.java:64)

        at com.ibatis.sqlmap.engine.execution.SqlExecutor.handleResults(SqlExecutor.java:385)

        at com.ibatis.sqlmap.engine.execution.SqlExecutor.handleMultipleResults(SqlExecutor.java:300)

        at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQuery(SqlExecutor.java:189)

        at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteQuery(MappedStatement.java:221)

        at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:189)

        ... 23 more

Caused by: java.lang.IllegalArgumentException

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

        at java.lang.reflect.Method.invoke(Method.java:597)

        at com.ibatis.common.beans.MethodInvoker.invoke(MethodInvoker.java:17)

        at com.ibatis.sqlmap.engine.accessplan.PropertyAccessPlan.setProperties(PropertyAccessPlan.java:46)

        ... 31 more

经检测代码后发现，是由于数据库表字段为null,而变量的set及get方法类型为long,发现了前人的一个bug，哈哈，顺便自己也学习了下。

ibatis当为原生数据类型时应该对字段为null时的处理，即添加nullValue="0"，long为原生数据，而Long不是。

 

 

正确的配置如下：

sqlmap文件如下：

 

&lt;typeAlias alias="jobRecClass"

type="com.alibaba.dw.powertrace.common.zephyr.common.JobRec" /&gt;

 

 

&lt;resultMap id="jobRecResult" class="jobRecClass"&gt;

&lt;result property="jobID" column="JOB\_ID" jdbcType="DECIMAL" /&gt;

&lt;result property="jobName" column="JOB\_NAME" jdbcType="VARCHAR" /&gt;

&lt;result property="jobMethodName" column="JOB\_METHOD\_NAME"

jdbcType="VARCHAR" /&gt;

&lt;result property="timeout" column="JOB\_TIMEOUT" jdbcType="DECIMAL" /&gt;

&lt;result property="slaveName" column="SLAVE\_NAME" jdbcType="VARCHAR" /&gt;

&lt;result property="startDate" column="START\_DATE" jdbcType="TIMESTAMP" /&gt;

&lt;result property="endDate" column="END\_DATE" jdbcType="TIMESTAMP" /&gt;

&lt;result property="jobStatus" column="JOB\_STATUS" jdbcType="VARCHAR" /&gt;

&lt;result property="redo" column="REDO" jdbcType="VARCHAR" /&gt;

&lt;result property="jobRunMsg" column="JOB\_RUN\_MSG" jdbcType="VARCHAR" /&gt;

&lt;result property="jobBeanName" column="JOB\_BEAN\_NAME" jdbcType="VARCHAR" /&gt;

&lt;result property="jobArgs" column="JOB\_ARGS" jdbcType="CLOB" /&gt;

&lt;result property="insDate" column="INS\_DATE" jdbcType="TIMESTAMP" /&gt;

&lt;result property="quota" column="JOB\_QUOTA" jdbcType="DECIMAL" /&gt;

&lt;result property="limitType" column="LIMIT\_TYPE" jdbcType="VARCHAR" /&gt;

&lt;result property="maxLimitNum" column="MAX\_LIMIT\_NUM" jdbcType="DECIMAL" /&gt;

&lt;result property="userId" column="USER\_ID" jdbcType="DECIMAL" /&gt;

&lt;!--  如果siteId定义为long,则需在如下添加 nullValue="0"用于处理当字段为null时自动转成0,否则报上述错误 --&gt;

&lt;result property="siteId" column="SITE\_ID" jdbcType="DECIMAL" /&gt;

&lt;/resultMap&gt;

 

 

 

&lt;select id="getJobListByJobStatus" parameterClass="String"

resultMap="jobRecResult"&gt;

&lt;![CDATA[

select

  JOB\_ID,

  JOB\_NAME,

  JOB\_BEAN\_NAME,

  JOB\_METHOD\_NAME,

  JOB\_TIMEOUT,

  SLAVE\_NAME,

  JOB\_QUOTA,

  JOB\_ARGS,

  JOB\_STATUS,

  INS\_DATE,

  REDO,

  START\_DATE,

  END\_DATE,

  LIMIT\_TYPE,

            MAX\_LIMIT\_NUM,

  JOB\_RUN\_MSG,

USER\_ID,

SITE\_ID

from

PT\_JOB\_REC 

where

JOB\_STATUS = #value#

order by JOB\_ID asc

]]&gt;

&lt;/select&gt;

 

 

com.alibaba.dw.powertrace.common.zephyr.common.JobRec.java如下：

 

/\**

 * Project: alibaba-dw-powertrace-common-zephyr

 * 

 * File Created at 2009-11-15

 * $Id$

 * 

 * Copyright 2008 Alibaba.com Croporation Limited.

 * All rights reserved.

 *

 * This software is the confidential and proprietary information of

 * Alibaba Company. ("Confidential Information").  You shall not

 * disclose such Confidential Information and shall use it only in

 * accordance with the terms of the license agreement you entered into

 * with Alibaba.com.

 \*/

package com.alibaba.dw.powertrace.common.zephyr.common;

 

import java.util.Date;

 

public class JobRec {

    private Long    jobID;

    private String  jobName;

    private String  jobBeanName;

    private String  jobMethodName;

    private String  jobArgs;

    private Long    timeout;

    private String  jobStatus;

    private String  slaveName;

    private Date    insDate;

    private Date    startDate;

    private Date    endDate;

    private String  JobRunMsg;

    private Integer quota;

    private Boolean redo;

    private String  limitType;

    private int     maxLimitNum;

    private Long    userId;

    private Long    siteId;

 

    /\**

     * 当Slave服务器宕机的时候，Job是否可以自动在另一台Slave服务器上运行。

     * 

     * @return

     \*/

    public Boolean isRedo() {

        return redo;

    }

 

    /\**

     * 在前段展示的时候使用，Json自动产生的时候需要使用这个方法。

     * 

     * @return

     \*/

    public Boolean getRedo() {

        return redo;

    }

 

    public void setRedo(Boolean redo) {

        this.redo = redo;

    }

 

    public String getJobName() {

        return jobName;

    }

 

    public void setJobName(String jobName) {

        this.jobName = jobName;

    }

 

    public String getJobMethodName() {

        return jobMethodName;

    }

 

    public void setJobMethodName(String jobMethodName) {

        this.jobMethodName = jobMethodName;

    }

 

    public Long getTimeout() {

        return timeout;

    }

 

    public void setTimeout(Long timeout) {

        this.timeout = timeout;

    }

 

    public String getSlaveName() {

        return slaveName;

    }

 

    public void setSlaveName(String slaveName) {

        this.slaveName = slaveName;

    }

 

    public Integer getQuota() {

        return quota;

    }

 

    public void setQuota(Integer quota) {

        this.quota = quota;

    }

 

    public Long getJobID() {

        return jobID;

    }

 

    public void setJobID(Long jobID) {

        this.jobID = jobID;

    }

 

    public String getJobBeanName() {

        return jobBeanName;

    }

 

    public void setJobBeanName(String jobName) {

        this.jobBeanName = jobName;

    }

 

    public String getJobArgs() {

        return jobArgs;

    }

 

    public void setJobArgs(String jobArgs) {

        this.jobArgs = jobArgs;

    }

 

    public String getJobStatus() {

        return jobStatus;

    }

 

    public void setJobStatus(String jobStatus) {

        this.jobStatus = jobStatus;

    }

 

    public Date getInsDate() {

        return insDate;

    }

 

    public void setInsDate(Date insDate) {

        this.insDate = insDate;

    }

 

    public Date getStartDate() {

        return startDate;

    }

 

    public void setStartDate(Date startDate) {

        this.startDate = startDate;

    }

 

    public Date getEndDate() {

        return endDate;

    }

 

    public void setEndDate(Date endDate) {

        this.endDate = endDate;

    }

 

    public String getJobRunMsg() {

        return JobRunMsg;

    }

 

    public void setJobRunMsg(String jobRunMsg) {

        JobRunMsg = jobRunMsg;

    }

 

    /\**

     * 获得Job的限制类型

     * 

     * @return

     \*/

    public String getLimitType() {

        return limitType;

    }

 

    /\**

     * 设置Job的限制类型，默认为normal

     * 

     * @param limitType

     \*/

    public void setLimitType(String limitType) {

        this.limitType = limitType;

    }

 

    /\**

     * 设置该类型的Job同时在Slave机器上最大的执行个数

     * 

     * @return

     \*/

    public int getMaxLimitNum() {

        return maxLimitNum;

    }

 

    /\**

     * 设置该类型的Job同时在Slave机器上最大的执行个数。默认为无限制，-1

     * 

     * @param maxLimitNum

     \*/

    public void setMaxLimitNum(int maxLimitNum) {

        this.maxLimitNum = maxLimitNum;

    }

 

    /\**

     * 获取用户的Id

     * 

     * @return

     \*/

    public Long getUserId() {

        return userId;

    }

 

    /\**

     * 设置用户的Id

     * 

     * @param userId

     \*/

    public void setUserId(Long userId) {

        this.userId = userId;

    }

 

    /\**

     * 获取用户的站点ID

     * 

     * @return

     \*/

    //此处该用long,前人误写成了long

    public Long getSiteId() {

        return siteId;

    }

 

    /\**

     * 设置用户的站点ID

     * 

     * @param userId

     \*/

 //此处该用long,前人误写成了long

    public void setSiteId(Long siteId) {

        this.siteId = siteId;

    }

}