---
title: "iBatis 中一对多时要注意的问题"
author: "simon_zhang"
date: "2006-08-25T17:06:00Z"
tags: []
slug: "ibatis"
draft: false
lastmod: "2006-08-25T17:06:00Z"
id: 64935
url: "https://mogee.iteye.com/blog/64935"
---

出现的问题：

2006-08-25 16:41:40 \[com.opensymphony.oscache.base.Config]-\[DEBUG] Config() called  
2006-08-25 16:41:40 \[com.opensymphony.oscache.base.Config]-\[DEBUG] Getting Config  
2006-08-25 16:41:40 \[com.opensymphony.oscache.base.Config]-\[INFO] Properties {cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache, cache.capacity=1000, cache.memory=true, cache.event.listeners=com.opensymphony.oscache.extra.ScopeEventListenerImpl}  
2006-08-25 16:41:40 \[com.opensymphony.oscache.base.AbstractCacheAdministrator]-\[DEBUG] Constructed AbstractCacheAdministrator()  
2006-08-25 16:41:40 \[com.opensymphony.oscache.general.GeneralCacheAdministrator]-\[INFO] Constructed GeneralCacheAdministrator()  
2006-08-25 16:41:40 \[com.opensymphony.oscache.general.GeneralCacheAdministrator]-\[INFO] Creating new cache  
2006-08-25 16:41:40 \[java.sql.Connection]-\[DEBUG] {conn-100000} Connection  
2006-08-25 16:41:41 \[java.sql.PreparedStatement]-\[DEBUG] {pstm-100001} PreparedStatement:    select id,name,sex from user where id=?   
2006-08-25 16:41:41 \[java.sql.PreparedStatement]-\[DEBUG] {pstm-100001} Parameters: \[1]  
2006-08-25 16:41:41 \[java.sql.PreparedStatement]-\[DEBUG] {pstm-100001} Types: \[java.lang.String]  
2006-08-25 16:41:41 \[java.sql.ResultSet]-\[DEBUG] {rset-100002} ResultSet  
2006-08-25 16:41:41 \[java.sql.ResultSet]-\[DEBUG] {rset-100002} Header: \[id, name, sex, id]  
2006-08-25 16:41:41 \[java.sql.ResultSet]-\[DEBUG] {rset-100002} Result: \[1, anna li, 5, 1]  
com.ibatis.common.jdbc.exception.NestedSQLException:    
\--- The error occurred in com/ibatis/demo/sqlmap/map/User.xml.   
\--- The error occurred while applying a result map.   
\--- Check the User.get-user-result.   
\--- The error happened while setting a property on the result object.   
\--- Cause: com.ibatis.common.exception.NestedRuntimeException: Error setting properties of ['com.ibatis.demo.User@116ab4e'](mailto:'com.ibatis.demo.User@116ab4e').  Cause: java.lang.IllegalArgumentException: argument type mismatch  
Caused by: java.lang.IllegalArgumentException: argument type mismatch  
Caused by: com.ibatis.common.exception.NestedRuntimeException: Error setting properties of ['com.ibatis.demo.User@116ab4e'](mailto:'com.ibatis.demo.User@116ab4e').  Cause: java.lang.IllegalArgumentException: argument type mismatch  
Caused by: java.lang.IllegalArgumentException: argument type mismatch  
 at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:188)  
 at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForList(GeneralStatement.java:123)  
 at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:610)  
 at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:584)  
 at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:101)  
 at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.queryForList(SqlMapClientImpl.java:78)  
 at com.ibatis.demo.UserDao.getUsers(UserDao.java:92)  
 at com.ibatis.demo.UserDao.main(UserDao.java:144)  
Caused by: com.ibatis.common.exception.NestedRuntimeException: Error setting properties of ['com.ibatis.demo.User@116ab4e'](mailto:'com.ibatis.demo.User@116ab4e').  Cause: java.lang.IllegalArgumentException: argument type mismatch  
Caused by: java.lang.IllegalArgumentException: argument type mismatch  
 at com.ibatis.sqlmap.engine.accessplan.PropertyAccessPlan.setProperties(PropertyAccessPlan.java:51)  
 at com.ibatis.sqlmap.engine.exchange.JavaBeanDataExchange.setData(JavaBeanDataExchange.java:112)  
 at com.ibatis.sqlmap.engine.mapping.result.BasicResultMap.setResultObjectValues(BasicResultMap.java:346)  
 at com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback.handleResultObject(RowHandlerCallback.java:63)  
 at com.ibatis.sqlmap.engine.execution.SqlExecutor.handleResults(SqlExecutor.java:395)  
 at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQuery(SqlExecutor.java:185)  
 at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.sqlExecuteQuery(GeneralStatement.java:205)  
 at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:173)  
 ... 7 more  
Caused by: java.lang.IllegalArgumentException: argument type mismatch  
 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:585)  
 at com.ibatis.sqlmap.engine.accessplan.PropertyAccessPlan.setProperties(PropertyAccessPlan.java:45)  
 ... 14 more

package com.ibatis.demo;

import java.io.Serializable;  
import java.util.ArrayList;  
//import java.util.HashSet;  
import java.util.List;  
//import java.util.Set;

public class User implements Serializable{

 /\**  
  *  
  \*/  
 private Integer id;  
 private String name;  
 private Integer sex;  
 private List addresses = new ArrayList();  
 //iBatis2.0 仅对java.util.List或java.util.Collection类型进行支持。  
 //原文：(载自：iBatis SQLMap2-cn.pdf文档)唯一的不同是，让SQL Map架构装入复杂类型集合（List）的业务对象的属性必须是java.util.List或java.util.Collection类型  
 //private Set addresses = new HashSet();  
 public User(){  
 }  
 public Integer getId() {  
  return id;  
 }  
 public void setId(Integer id) {  
  this.id = id;  
 }  
 public String getName() {  
  return name;  
 }  
 public void setName(String name) {  
  this.name = name;  
 }  
 public Integer getSex() {  
  return sex;  
 }  
 public void setSex(Integer sex) {  
  this.sex = sex;  
 }  
// public Set getAddresses() {  
//  return addresses;  
// }  
// public void setAddresses(Set addresses) {  
//  this.addresses = addresses;  
// }  
 public void setAddresses(List addresses) {  
  this.addresses = addresses;  
 }  
 public List getAddresses() {  
  return addresses;  
 }  
   
}

 

&lt;?xml version="1.0" encoding="UTF-8" ?&gt;

&lt;!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"  
    "[http://ibatis.apache.org/dtd/sql-map-2.dtd](http://ibatis.apache.org/dtd/sql-map-2.dtd)"&gt;  
&lt;sqlMap namespace="User"&gt;  
 &lt;typeAlias alias="user" type="com.ibatis.demo.User" /&gt;  
 &lt;typeAlias alias="string" type="java.lang.String" /&gt;  
 &lt;typeAlias alias="address" type="com.ibatis.demo.Address"/&gt;  
 &lt;cacheModel id="userCache" type="OSCACHE"&gt;  
  &lt;flushInterval hours="24" /&gt;  
  &lt;flushOnExecute statement="updateUser" /&gt;  
  &lt;flushOnExecute statement="insertUser" /&gt;  
  &lt;flushOnExecute statement="delUser" /&gt;  
  &lt;flushOnExecute statement="deleteUser" /&gt;  
  &lt;property name="size" value="1000" /&gt;  
 &lt;/cacheModel&gt;  
 &lt;resultMap id="get-user-result" class="user"&gt;  
  &lt;result property="id" column="id"/&gt;  
  &lt;result property="name" column="name"/&gt;  
  &lt;result property="sex" column="sex"/&gt;  
  &lt;result property="addresses" column="id" select="getAddressByUserId"/&gt;  
 &lt;/resultMap&gt;  
 &lt;resultMap id="get-address-result" class="address"&gt;  
  &lt;!-- result property="userid" column="userid"/--&gt;  
  &lt;result property="address" column="address"/&gt;  
  &lt;result property="zipcode" column="zipcode"/&gt;  
 &lt;/resultMap&gt;  
 &lt;select id="getUsers" parameterClass="string" resultMap="get-user-result"&gt;  
  select id,name,sex from user where id=#id#  
 &lt;/select&gt;  
 &lt;select id="getAddressByUserId" parameterClass="int" resultMap="get-address-result"&gt;  
  select address,zipcode from address where userid = #id#  
 &lt;/select&gt;  
   
 &lt;select id="getUser" parameterClass="string" cacheModel="userCache"  
  resultClass="user"&gt;  
  select name,sex from user where name=#name#  
 &lt;/select&gt;  
 &lt;update id="updateUser" parameterClass="user"&gt;  
  update user set name=#name#, sex=#sex# where id=#id#  
 &lt;/update&gt;  
 &lt;insert id="insertUser" parameterClass="user"&gt;  
  insert into user( name,sex) values(#name#,#sex#)  
 &lt;/insert&gt;  
 &lt;delete id="deleteUser" parameterClass="string"&gt;  
  delete from user where id=#id#  
 &lt;/delete&gt;  
 &lt;delete id="delUser" parameterClass="user"&gt;  
  delete from user where id=#id#  
 &lt;/delete&gt;  
 &lt;select id="getUserList" parameterClass="string"  
  cacheModel="userCache" resultClass="user"&gt;  
  select id ,name,sex from user where name like #name#  
 &lt;/select&gt;  
&lt;/sqlMap&gt;