-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Possible NullPointerException when assigning generated keys to objects with custom hashCode() method #1719
Comments
Thank you for the report, @WeiYang1005 . |
Here is a demo project to reproduce it, https://github.com/WeiYang1005/mybatis-issue . |
I found many issues the same reason, like #1716, and #1711, both because the method getAssignerForParamMap() in Jdbc3KeyGenerator.java uses distinct() of stream to compare input params, while the params contain a null id. |
So, the custom |
I think your implementation of |
To detect single parameter case, it now checks parameter name instead of checking equality of parameter objects. This should fix mybatisgh-1719.
When detecting single parameter case, it now checks parameter name instead of equality of the parameter objects. This should fix mybatisgh-1719. A caveat : when there is only one parameter and its name is 'param2' (for whatever reason), `keyProperty` must include the parameter name i.e. `keyProperty="param2.xx"`.
There seem to be cases that an exception is thrown intentionally from within equals/hashCode. |
You are right, that is not a good implementation of equals and hashcode. We have already changed the implementation, but this kind of implementation mostly are old codes, we cann't prevent others' existed mistakes :D. Thus we want to avoid such cases or get more useful feedbacks. Thanks for your apply! GL! |
When detecting single parameter case, it now checks parameter name instead of equality of the parameter objects. This should fix mybatisgh-1719. A caveat : when there is only one parameter and its name is 'param2' (for whatever reason), `keyProperty` must include the parameter name i.e. `keyProperty="param2.xx"`.
Hi all, The workaround is merged. =D |
When detecting single parameter case, it now checks parameter name instead of equality of the parameter objects. This should fix mybatisgh-1719. A caveat : when there is only one parameter and its name is 'param2' (for whatever reason), `keyProperty` must include the parameter name i.e. `keyProperty="param2.xx"`.
##Summary
Hello MyBatis, here is a bug for inserting objects of List which overrides hashcode method and the key uses as hashcode identifier (like id) is null.
Person overrides hashcode by id , id is null, insert List in mybatis leads to NullPointerException because dinstinct compare with id which is null.
MyBatis version
3.5.1
Database vendor and version
5.7.1
Test case or example project
Person.java
insertService.java
mapper.xml
Steps to reproduce
While we request insertBatch, mybatis will throw a 'java.lang.NullPointerException' exception. because in src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java line 174, it uses paramMap.values().stream().distinct().count() == 1 to adjust whether the count of params is 1, but the method distinct() of stream in jdk8 uses hashcode of object to do distinct, when the object overrides hashcode method with the hashcode of id, and the insert operation doesn't have any content in field id, the exception then occrupt.
But in mybatis before version 3.4.6(I have ensured), this never happened, because Jdbc3KeyGenerator in mybatis 3.4.6 didn't use distinct() method to adjust the count of param.
Anyway, in most cases, we will use this logic to insert batch datas ,hope you can fix this bug in the future.
Expected result
true
Actual result
java.lang.NullPointerException
You can contact me by 346736752@qq.com. Thank you!
The text was updated successfully, but these errors were encountered: