myBATIS 3 dynamic sql 'foreach' list got problem

6,883 views
Skip to first unread message

bruce666

unread,
Jun 8, 2010, 10:28:54 PM6/8/10
to mybatis-user
I used mybatis 3.1 ga,

those are my JavaBean classes,

<b>
public class SearchObject {
private Long id;
private List<Long> childIdList = new ArrayList<Long>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Long> getChildIdList () {
return childIdList;
}
public void setChildIdList (List<Long> childIdList ) {
this.childIdList = childIdList ;
}
}

public class ResultObject {
private String result;
public String getresult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
</b>

and this is my sqlmapping file,

<i><select id="getView" parameterType="SearchObject"
resultType="ResultObject">
select o.result from table o
<where>
<if test="id != null ">
o.id = #{id}
</if>
<if test="childIdList.size() > 0">
and o.child in
<b><foreach item="item" index="index" open="(" close=")"
separator="," collection="childIdList" >
#{item}
</foreach></b>
</if>
</where>
</select></i>

when I execute I got error:

<b>### Error querying database. Cause:
org.apache.ibatis.reflection.ReflectionException: There is no getter
for property named '__frch_item_0' in 'SearchObject'
### The error may involve sqlmap.Search.getView-Inline
### The error occurred while setting parameters
### Cause: org.apache.ibatis.reflection.ReflectionException: There is
no getter for property named '__frch_item_0' in 'SearchObject'
ERROR [STDERR] org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause:
org.apache.ibatis.reflection.ReflectionException: There is no getter
for property named '__frch_item_0' in 'SearchObject'
### The error may involve sqlmap.CCSalesOrderItem.getCCSOItemView-
Inline
### The error occurred while setting parameters
### Cause: org.apache.ibatis.reflection.ReflectionException: There is
no getter for property named '__frch_item_0' in 'SearchObject'</b>

If I change the sql mapping to this,

<i><select id="getView" parameterType="SearchObject "
resultType="ResultObject">
select o.result from table o
<where>
<if test="id != null ">
o.id = #{id}
</if>
<if test="childIdList.size() > 0">
and o.child in
<b><foreach item="item" index="index" open="(" close=")"
separator="," collection="childIdList" >
#childIdList[${index}]
</foreach></b>
</if>
</where>
</select></i>

I got this error:

<b>### Error querying database. Cause:
org.apache.ibatis.executor.ExecutorException: There was no TypeHandler
found for parameter childIdList[0] of statement sqlmap.Search.getView
### The error may involve sqlmap.Search.getView-Inline
### The error occurred while setting parameters
### Cause: org.apache.ibatis.executor.ExecutorException: There was no
TypeHandler found for parameter childIdList[0] of statement
sqlmap.Search.getView</b>

and how to do foreach with enum?

now I use this to run ,
<i><b>#{childIdList[${index}], javaType=Long}

#{enmuLilst[${index}], javaType=myEnum} </b></i>

Do I have other way?

bruce666

unread,
Jun 8, 2010, 10:31:11 PM6/8/10
to mybatis-user
I used mybatis 3.1 ga,

those are my JavaBean classes,

public class SearchObject {
private Long id;
private List<Long> childIdList = new ArrayList<Long>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Long> getChildIdList () {
return childIdList;
}
public void setChildIdList (List<Long> childIdList ) {
this.childIdList = childIdList ;
}
}

public class ResultObject {
private String result;
public String getresult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}

and this is my sqlmapping file,

<select id="getView" parameterType="SearchObject"
resultType="ResultObject">
select o.result from table o
<where>
<if test="id != null ">
o.id = #{id}
</if>
<if test="childIdList.size() > 0">
and o.child in
<foreach item="item" index="index" open="(" close=")"
separator="," collection="childIdList" >
#{item}
</foreach>
</if>
</where>
</select>

when I execute I got error:

### Error querying database. Cause:
org.apache.ibatis.reflection.ReflectionException: There is no getter
for property named '__frch_item_0' in 'SearchObject'
### The error may involve sqlmap.Search.getView-Inline
### The error occurred while setting parameters
### Cause: org.apache.ibatis.reflection.ReflectionException: There is
no getter for property named '__frch_item_0' in 'SearchObject'
ERROR [STDERR] org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause:
org.apache.ibatis.reflection.ReflectionException: There is no getter
for property named '__frch_item_0' in 'SearchObject'
### The error may involve sqlmap.CCSalesOrderItem.getCCSOItemView-
Inline
### The error occurred while setting parameters
### Cause: org.apache.ibatis.reflection.ReflectionException: There is
no getter for property named '__frch_item_0' in 'SearchObject'

If I change the sql mapping to this,

<select id="getView" parameterType="SearchObject "
resultType="ResultObject">
select o.result from table o
<where>
<if test="id != null ">
o.id = #{id}
</if>
<if test="childIdList.size() > 0">
and o.child in
<foreach item="item" index="index" open="(" close=")"
separator="," collection="childIdList" >
#childIdList[${index}]
</foreach>
</if>
</where>
</select>

I got this error:

### Error querying database. Cause:
org.apache.ibatis.executor.ExecutorException: There was no TypeHandler
found for parameter childIdList[0] of statement sqlmap.Search.getView
### The error may involve sqlmap.Search.getView-Inline
### The error occurred while setting parameters
### Cause: org.apache.ibatis.executor.ExecutorException: There was no
TypeHandler found for parameter childIdList[0] of statement
sqlmap.Search.getView

and how to do foreach with enum?

now I use this to run ,
#{childIdList[${index}], javaType=Long}

#{enmuLilst[${index}], javaType=myEnum}

Guy Rouillier

unread,
Jun 8, 2010, 11:34:09 PM6/8/10
to mybati...@googlegroups.com
I'm using foreach successfully with type Integer[]. Are you using a
Java mapper file? If so, show the method declaration there, and also
show the code that invokes that method. Perhaps there is an issue with
how you are setting up or invoking the mapper function.


--
Guy Rouillier

bruce b

unread,
Jun 9, 2010, 4:47:14 AM6/9/10
to mybati...@googlegroups.com
no, I did not use Java mapping file, and I found  mybatis code like this :

org.apache.ibatis.builder.SqlSourceBuilder

line:58
......
} else if (metaClass.hasGetter(property)) {
        propertyType = metaClass.getGetterType(property);
      } else {
        propertyType = Object.class;
      }
........

when build dynamic sql, if my fields are in a list, and I use #{item} to foreach,  there has no get method, the propertyType may set outer object. if my fields are in a list, and I use #{list[${index}]} to foreach, the propertyType may set List.
So I need declare the javaType in mapping file....

is it right?
Reply all
Reply to author
Forward
0 new messages