Search code examples
javamysqlibatismybatis

While inserting the records in mysql table getting exception


I am inserting values to one of mysql table and I am getting following exception:

Could not set property 'id' of 'class com.mycom.myproject.db.mybatis.model.FeedEntry' with    value '2' Cause: java.lang.IllegalArgumentException: java.lang.ClassCastException@175e216] with root cause.

Record is getting inserted successfully in table. In my table id is auto-increment and primary key with not null. In my mapper class I have

 <insert id="insertSelective" parameterType="com.mycom.myproject.db.mybatis.model.Category" >
<!--
  WARNING - @mbggenerated
  This element is automatically generated by MyBatis Generator, do not modify.
  This element was generated on Fri Aug 10 11:40:59 BST 2012.
-->
<selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER" >
  SELECT LAST_INSERT_ID()
</selectKey>
insert into category
<trim prefix="(" suffix=")" suffixOverrides="," >
  <if test="fSourceId != null" >
    f_source_id,
  </if>
  <if test="userId != null" >
    user_id,
  </if>
  <if test="author != null" >
    author,
  </if>
  <if test="name != null" >
    name,
  </if>

</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
  <if test="fSourceId != null" >
    #{fSourceId,jdbcType=INTEGER},
  </if>
  <if test="userId != null" >
    #{userId,jdbcType=BIGINT},
  </if>
  <if test="author != null" >
    #{author,jdbcType=VARCHAR},
  </if>
  <if test="name != null" >
    #{name,jdbcType=VARCHAR},
  </if>      
</trim>

so I don't understand why I am getting the above exception if everything is all right.

Please let me know if I missed anything.


Solution

  • MyBatis is trying to call method "setId" on class FeedEntry with parameter of java.lang.Integer and the result is a ClassCastException.

    You need to ensure the result type of the select key gives correct type.