Skip to content

mybatis lose duplicated rows when using association #522

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

Closed
gaofeng816 opened this issue Nov 11, 2015 · 1 comment
Closed

mybatis lose duplicated rows when using association #522

gaofeng816 opened this issue Nov 11, 2015 · 1 comment

Comments

@gaofeng816
Copy link

I have a resultMap definition:

<select id="querySellerSupplierNotice" resultType="Message">
    SELECT
    a.id,
    a.create_time as message_create_time,
    a.content,
    b.title,
    c.subdomain,
    b.id as bid,
    c.id as cid
    FROM
    message a
    INNER JOIN supplier b
    ON
    a.user_id = b.user_id
    LEFT JOIN site c
    ON
    a.user_id = c.user_id
    INNER JOIN ln_seller2product d
    ON a.user_id =
    d.supplier_user_id
    WHERE a.type
    = 'corpnews'
    AND a.create_time >
    date_sub(curdate(), interval 90 day)
    AND a.STATE = 1
    AND d.seller_user_id =
    #{sellerId}
    ORDER BY
    a.create_time DESC,b.title DESC
  </select>

<resultMap type="Message" id="sellerSupplierMap">
    <id property="id" column="id" />
    <result property="content" column="content" />
    <result property="createTime" column="message_create_time" />
    <association property="supplier" column="user_id" javaType="Supplier">
      <id property="id" column="bid" />
      <result property="title" column="title" />
    </association>
    <association property="site" column="user_id" javaType="Site">
      <id property="id" column="cid" />
      <result property="subdomain" column="subdomain" />
    </association>
  </resultMap>

this select will return over 70 rows but a lot of them are totally duplicated.

If I use resultType="hashmap" or resultType="Message" it will returns all rows.
But I use the resultMap above, mybatis will automatically ignore the duplicated rows, and returns a small list than expected.

I don't think it's appropriate for mybatis to drop rows under any scenario.

@harawata
Copy link
Member

It is the expected behavior when you use association or collection.
There is a simple explanation comment on the similar issue #512 .

If you want all the rows returned, just build a resultMap without association.

<resultMap type="Message" id="sellerSupplierMap">
  <id property="id" column="id" />
  <result property="content" column="content" />
  <result property="createTime" column="message_create_time" />
  <result property="supplier.id" column="bid" />
  <result property="supplier.title" column="title" />
  <result property="site.id" column="cid" />
  <result property="site.subdomain" column="subdomain" />
</resultMap>

[edited: wrong issue number]

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

2 participants