Mybatis-3: is this bug?

Created on 3 Nov 2015  ·  3Comments  ·  Source: mybatis/mybatis-3

Table name : HIST

| HISTID | MANAGER_NAME | USER_NAME | USER_EMAIL | USERID |
| --- | --- | --- | --- | --- |
| 1 | hong | chris | [email protected] | 100 |
| 2 | hong | evan | [email protected] | 200 |
| 3 | hong | jack | [email protected] | 300 |
| 4 | hong | mike | [email protected] | 400 |
| 5 | hong | ryon | [email protected] | 500 |

<resultMap id="userResult" type="xxx.xxx.User">
        <result property="userId" column="USERID" />
        <result property="userName" column="USER_NAME" />
        <result property="email" column="USER_EMAIL" />
</resultMap>

<resultMap id="result" type="xxx.xxx.Hist">
        <result property="id" column="HISTID" />    
        <result property="managerName" column="MANAGER_NAME" />     
        <association property="user" javaType="xxx.xxx.User" resultMap="userResult" />
</resultMap>

<select id="findItems" parameterType="xxx.xxx.Hist" resultMap="result">
        SELECT MANAGER_NAME from HIST
</select>

Java:

List<Hist> list = histMapper.findItems(item);
System.out.println("LIST SIZE : " + list.size());

result:
DEBUG: xxxx.xxxx.HistMapper.findItems - <== Total: 5
LIST SIZE : 1 (why??)

But, modified query as below.

<select id="findItems" parameterType="xxx.xxx.Hist" resultMap="result">
                -- Added column HISTID
        SELECT HISTID, MANAGER_NAME from HIST
</select>

success!

or modified resultMap as below.

<resultMap id="userResult" type="xxx.xxx.User">
        <result property="userId" column="USERID" />
        <result property="userName" column="USER_NAME" />
        <result property="email" column="USER_EMAIL" />
    </resultMap>

    <resultMap id="result" type="xxx.xxx.Hist">
        <result property="id" column="HISTID" />    
        <result property="managerName" column="MANAGER_NAME" />     
                // removed association
        <-- association property="user" javaType="xxx.xxx.User" resultMap="userResult" -->
</resultMap>

success.

is this bug?

Most helpful comment

I don't think this is a bug. For result maps with associations and collections MyBatis de-duplicates the result set according to the <id/> columns if present, otherwise every <result/> column. This is a very important feature when it comes to constructing a complex object model from a single result set. (i.e. using fetch joins to avoid the N+1 selects problem).

I'm assuming the column HISTID is the primary key of the HIST table. HISTID is not in your result set however, only MANAGER_NAME, which is apparently not unique. I would recommend to use a different <resultMap/> without associations or collections, or to include HISTID in your result set, by selecting it. For performance reasons, you should consider using <id/> instead of <result/> for the primary key columns.

All 3 comments

I don't think this is a bug. For result maps with associations and collections MyBatis de-duplicates the result set according to the <id/> columns if present, otherwise every <result/> column. This is a very important feature when it comes to constructing a complex object model from a single result set. (i.e. using fetch joins to avoid the N+1 selects problem).

I'm assuming the column HISTID is the primary key of the HIST table. HISTID is not in your result set however, only MANAGER_NAME, which is apparently not unique. I would recommend to use a different <resultMap/> without associations or collections, or to include HISTID in your result set, by selecting it. For performance reasons, you should consider using <id/> instead of <result/> for the primary key columns.

agree with @mches , you need id or enough property mappings to uniquely identify each result row

agree with @mches , you need id or enough property mappings to uniquely identify each result row

agree with you,its useful to me too!

Was this page helpful?
0 / 5 - 0 ratings