Skip to content
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

cannot find type handler for enum when enum value has methods #1489

Closed
zhzhsx opened this Issue Mar 4, 2019 · 3 comments

Comments

Projects
None yet
2 participants
@zhzhsx
Copy link
Contributor

zhzhsx commented Mar 4, 2019

MyBatis version

3.5.0

Database vendor and version

Independent of databases and versions. For an exapmle, hsqldb in mybatis tests.

Test case or example project

https://github.com/zhzhsx/mybatis-3/tree/master/src/test/java/org/apache/ibatis/submitted/enum_with_method

Steps to reproduce

Run the test: EnumWithMethodTest#shouldInsertAUser.

Expected result

A user should be inserted into the database successfully.

Actual result

The test fails.

Notes on conditions to reproduce the issue

  • The enum value (Currency enum in the test) has methods
  • No 'parameterType' attribute is provided in corresponding insert mapper
@kazuki43zoo

This comment has been minimized.

Copy link
Member

kazuki43zoo commented Mar 4, 2019

@zhzhsx

Thanks for your reporting! I think this is bug.
MyBatis will try to resolve automatically using actual object type at request time when parameterType attribute is omit on XML element. In this case(enum that implements any method), object type become an anonymous class (such as z.yz.Currency$1). Therefore MyBatis does not use the EnumTypeHandler because the anonymous class return false on Class#isEnum().

@kazuki43zoo kazuki43zoo self-assigned this Mar 4, 2019

@kazuki43zoo kazuki43zoo added the bug label Mar 4, 2019

@kazuki43zoo kazuki43zoo added this to the 3.5.1 milestone Mar 4, 2019

kazuki43zoo added a commit to kazuki43zoo/mybatis-3 that referenced this issue Mar 4, 2019

kazuki43zoo added a commit to kazuki43zoo/mybatis-3 that referenced this issue Mar 4, 2019

@kazuki43zoo

This comment has been minimized.

Copy link
Member

kazuki43zoo commented Mar 4, 2019

Workaround is adding the parameterType attribute in XML element(insert/update/delete/select). or specify typehandler in bind parameter expression(e.g. #{currency,typeHandler=...EnumTypeHandler}).

kazuki43zoo added a commit to kazuki43zoo/mybatis-3 that referenced this issue Mar 4, 2019

Polishing for mybatis#1489
* Change to use isAssignableFrom method
* Add local variable to hold Enum class

kazuki43zoo added a commit to kazuki43zoo/mybatis-3 that referenced this issue Mar 4, 2019

kazuki43zoo added a commit that referenced this issue Mar 4, 2019

Merge pull request #1490 from kazuki43zoo/gh-1489-enum-with-method
Support anonymous enum object that implement method
@kazuki43zoo

This comment has been minimized.

Copy link
Member

kazuki43zoo commented Mar 4, 2019

@zhzhsx

It should be fixed in the latest 3.5.1-SNAPSHOT.
Thanks you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.