org.apache.ibatis.exceptions.TooManyResultsException when using type="hashmap"

786 views
Skip to first unread message

User_555

unread,
Mar 26, 2012, 4:36:26 PM3/26/12
to mybatis-user
Hi,

I am trying to get my result set as a HashMap as <userid, userObject>
map, reprseting all the users from the table. Below is my mapper
config and selectmethod from Mapper class. It works fine if there is
only one row. If there are more than one row returned it throws too
many results exception. I am very very new to iBatis/myBatis (started
just last week).
Can some one please advice what's causing this and how to fix this?

===== usermapper.xml ========
<resultMap id="usersResultMap" type="hashmap">
<id column="userid" property="key" jdbcType="VARCHAR" />
<association property="value" column="last_name"
javaType="com.ge.wolc.lighting.db.model.WolcUser">
<id column="userid" property="userid" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" /
>
<result column="first_name" property="first_name"
jdbcType="VARCHAR" />
<result column="last_name" property="last_name"
jdbcType="VARCHAR" />
<result column="group_id_rank" property="group_id_rank"
jdbcType="INTEGER" />
<result column="max_pwd_attempts" property="max_pwd_attempts"
jdbcType="INTEGER" />
<result column="active" property="active" jdbcType="CHAR" />
<result column="locked" property="locked" jdbcType="CHAR" />
<result column="time_add" property="time_add" jdbcType="DATE" />
<result column="time_mod" property="time_mod" jdbcType="DATE" /
>
</association>
</resultMap>

<select id="selectAllUsers" resultMap="usersResultMap">
select
<include refid="Base_Column_List" />
from wolc_users
</select>

==== abstract method from usermapper interface is something like below
=============
Map<String, WolcUser> selectAllUsers();
tried below also
HashMap<String, WolcUser> selectAllUsers();

Eduardo Macarron

unread,
Mar 26, 2012, 5:06:02 PM3/26/12
to mybati...@googlegroups.com
One hashmap holds one object. So If you want to retrive a list you
should use this:

List<Map<String, WolcUser>> selectAllUsers();

User_555

unread,
Mar 27, 2012, 11:36:49 AM3/27/12
to mybatis-user
I was able to get the desired results by annotating it as below

@MapKey("userid") (an then change the resultmap's userid property
name to "userid" from "key").

Now I am able to get a HashMap (with all users NOT just 1 record) as
{userdid=(UserObject)}

On Mar 26, 5:06 pm, Eduardo Macarron <eduardo.macar...@gmail.com>
wrote:
> One hashmap holds one object. So If you want to retrive a list you
> should use this:
>
> List<Map<String, WolcUser>> selectAllUsers();
>

Dridi Boukelmoune

unread,
Mar 28, 2012, 12:59:10 AM3/28/12
to mybati...@googlegroups.com
Hi,

Could you post a sample code showing how to do this ?
I've always thought I could only get one Map per result.

Dridi

http://www.zenika.com/

Eduardo

unread,
Mar 29, 2012, 12:52:16 AM3/29/12
to mybatis-user
Morning Dridi,

First of all, nice post in Zenika's blog, I have linked it in google
code.

Maps are often a bit difficult to understand. MB3 provides three
possibilities:

- return a record inside a hashmap
- return a list of records inside a hashmap
- return a map of records inside beans

The first two are just using this:

public interface MyMapper{
@Select("select * from user where id=#{id}")
Map<String, Object> getUser(long id)
}

or using the xml syntax

<select id="getUser" returnType="map">
select * from user where id=#{id}
</select>

If you want to retrieve a list of maps, then the mapper should look
like:

public interface MyMapper{
@Select("select * from user where id=#{id}")
List<Map> getUser(long id)
}

The last option is to use the @MapKey annotation. This annotation lets
you retrieve multiple objects inside a Map instead of inside a List.
For that purpose you must specify what property will be used as a Key
(that is @MapKey for)

So this will retrieve a map of users

public interface MyMapper{
@MapKey("id")
@Select("select * from user where id=#{id}")
Map<Long, User> getUser(long id)
}

Hope I managed to explain it!

Dridi Boukelmoune

unread,
Apr 2, 2012, 6:25:34 AM4/2/12
to mybati...@googlegroups.com
Hey Eduardo!

What a surprise, that's very cool! I didn't intend to send you this link since it's in french (maybe you should actually mention this is french in the wiki)
btw where did you find my post ? can you read french or did you use a translator ?

Thx for your explanation.
From what I read, the @MapKey is what I'm looking for. Is there a way to do this with xml configuration ?

Thanks again, you made my day!

Dridi

http://www.zenika.com/

Eduardo

unread,
Apr 2, 2012, 11:39:53 AM4/2/12
to mybatis-user
Google translator does a very good job translating french to
spanish! :)

> From what I read, the @MapKey is what I'm looking for. Is there a way to do
> this with xml configuration ?

There is an API for that:
<K,V> Map<K,V> selectMap(String statement, Object parameter, String
mapKey)

Reply all
Reply to author
Forward
0 new messages