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
Closed

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

zhzhsx opened this issue Mar 4, 2019 · 3 comments
Assignees
Labels
Milestone

Comments

@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
Copy link
Member

@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
Copy link
Member

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
* 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
Support anonymous enum object that implement method
@kazuki43zoo
Copy link
Member

@zhzhsx

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

pulllock pushed a commit to pulllock/mybatis-3 that referenced this issue Oct 19, 2023
pulllock pushed a commit to pulllock/mybatis-3 that referenced this issue Oct 19, 2023
pulllock pushed a commit to pulllock/mybatis-3 that referenced this issue Oct 19, 2023
* Change to use isAssignableFrom method
* Add local variable to hold Enum class
pulllock pushed a commit to pulllock/mybatis-3 that referenced this issue Oct 19, 2023
pulllock pushed a commit to pulllock/mybatis-3 that referenced this issue Oct 19, 2023
…with-method

Support anonymous enum object that implement method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants