mybatis batch insert not writing back id

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

mybatis batch insert not writing back id

Rookiy Tan
I was using mybatis-spring to batch insert a list of objects into mysql.

Something like this:

<insert id="batchCreate" useGeneratedKeys="true" keyProperty="id" parameterType="java.util.ArrayList">
    INSERT INTO Customer(name, address, sex, created, updated)
    VALUES
   
<foreach collection="list" item="item" separator=",">
       (#{item.name}, #{item.address}, #{item.sex}, now(), now())
   
</foreach>
    ON DUPLICATE KEY UPDATE
    address = VALUES(address),
    sex = VALUES(sex),
    updated = now()
</insert>

When I call the mapper method, only the very first object of my list will be writed back with a id, rest of the items' id is still null.

After I remove the ON DUPLICATE KEY part , everything works fine.

I wonder if it's a bug or an intentionally design, how can i meet my needs with the unique key and id. 


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.