Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is this bug? #512

Closed
hanwooram opened this issue Nov 3, 2015 · 3 comments
Closed

is this bug? #512

hanwooram opened this issue Nov 3, 2015 · 3 comments

Comments

@hanwooram
Copy link

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?

@mches
Copy link

mches commented Nov 5, 2015

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.

@chanjarster
Copy link

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

@lidwt721
Copy link

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

agree with you,its useful to me too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants