简体   繁体   中英

MyBatis: getting id from inserted array of object returns error

I use mybatis 3.3.0-SNAPSHOT. I want to insert list of objects, and get id of every object. In interface I have:

public void createCore(@Param("cores")List<Object> cores);  

In xml mapper I have:

<insert id="createCore" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
  INSERT INTO mytable (raz,dva,tri )
  VALUES
    <foreach collection="cores" item="core" separator=",">
     (#{core.raz}, #{core.dva}, #{core.tri})
    </foreach>
</insert>

And I get:

Error updating database. Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated

key or setting result to parameter object. Cause: org.apache.ibatis.binding.BindingException: Parameter 'id' not found. Available parameters are [cores, param1]

I tried and keyProperty="core.id" but get the same error.

How to fix it?

这个问题在版本3.3.1中得到修复

Got that error too while trying to insert multiple rows with usegeneratedkeys.

The workaround fix I found was mentioned here: http://mybatis-user.963551.n3.nabble.com/MyBatys-Batch-Insert-doubt-error-retrieving-generated-id-binding-to-POJO-td4029977.html

All you need is to rename the param to "list". so "cores" -> "list" in the example above, and auto generated ids are working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM