Table name : HIST
HISTID | MANAGER_NAME | USER_NAME | USER_EMAIL | USERID |
---|---|---|---|---|
1 | hong | chris | chris@xxxx.com | 100 |
2 | hong | evan | evan@xxxx.com | 200 |
3 | hong | jack | jack@xxxx.com | 300 |
4 | hong | mike | mike@xxxx.com | 400 |
5 | hong | ryon | ryon@xxxx.com | 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?
Comment From: mches
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.
Comment From: chanjarster
agree with @mches , you need id or enough property mappings to uniquely identify each result row
Comment From: lidwt721
agree with @mches , you need id or enough property mappings to uniquely identify each result row
agree with you,its useful to me too!