Jdbc3KeyGenerator cannot get correct key property object

22 views
Skip to first unread message

Huang Dan

unread,
Nov 17, 2022, 9:06:17 AM11/17/22
to mybatis-user
Hi Team,
I recently upgrade mybatis from 3.4.6 to 3.5.10, then I got failure in my project. (no setter for keyProperty "id"...)
After debugging into the code, I found the issue was in Jdbc3KeyGenerator.

Background: 
  • I has a custom batchInsert method in xxMapper interface with a custom SQL in xml file(insert ... values ...). The xml SQL has properties of useGeneratedKeys="true" keyProperty="id". (my entity class does have a Long type id with @TableId(value = "id", type = IdType.AUTO) annotation)
  • The batchInsert in mapper method has an annotation of @Param(list) on the single parameter of ArrayList.
    • int batchInsert(@Param("list") List<Data> dataList);
  • I have a spring interceptor which will do some special logic before insert/update statement processing in mybatis Executor update method. Actually, the interceptor will add a property called "tenantId" to the paramMap if the param was MapperMethod.ParamMap type. The tenantId in the paramMap can be referenced  in XML SQL without need it passed in from mapper method.
  • Thus, when batchInsert execute into mybatis framework, the paramMap will contain three elements: list, param1, and "tenantId".
  • In Jdbc3KeyGenerator -> getAssignerForParamMap, the singleParam only check if there has no "param2", however, another param "tenantId" has been introduced in my interceptor. Thus, the "tenentId" (java.lang.Long) will be returned as the object which contains "keyProperty" and eventually failed in keyProperty setter check
  • Screenshot 2022-11-17 at 21.49.57.png
  • Screenshot 2022-11-17 at 21.55.29.png
  • Howevet, in mybatis 3.4.6, the code works fine.
  • Screenshot 2022-11-17 at 21.59.12.png
Forgive my poor English, could someone help to fix this issue?

Iwao AVE!

unread,
Nov 17, 2022, 9:03:25 PM11/17/22
to mybati...@googlegroups.com
Hello,

You should not modify the parameter map like that.
It interferes with the internal logic as you found out.

If you explain what you try to achieve by adding the extra parameter, we might be able to provide a better solution.
If, for example, it is for a multi-tenant application, the answers on this question might be helpful:

Regards,
Iwao

--
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 mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/946e9055-0b29-4911-ba66-06f3dec460c9n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages