EnumTypeHandler problem mybatis v3.5.11

137 views
Skip to first unread message

Brian Hurley

unread,
Apr 27, 2023, 11:34:27 AM4/27/23
to mybatis-user
I'm trying to implement ENUMs  rather than passing them as a string to mybatis using the EnymTypeHandler

Here is my ENUM

public enum UserStatus {


  ACTIVE,

  INACTIVE,

  DELETE


}


MODEL file has UserStatus defined as ‘status’


private UserStatus status = null;




In my userMapper.xml


RESULT MAP…

<result property="status" column="status" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/>


INSERT…

 #{status, typeHandler=org.apache.ibatis.type.EnumTypeHandler},


UPDATE…

status = #{status, typeHandler=org.apache.ibatis.type.EnumTypeHandler},



I get error that argument type mismatch.   The database fields have the String values of ‘ACTIVE’ || ‘INACTIVE’ etc.


Caused by: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'status' of 'class com.elsevier.topicpages.model.User' with value 'ACTIVE' Cause: java.lang.IllegalArgumentException: argum

ent type mismatch

at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:97)

at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:439)

at jdk.proxy2/jdk.proxy2.$Proxy65.selectList(Unknown Source)

at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224)



What am I missing on this?   Do I need to implement a specific EnumTypeHandler for UserStatus?

Iwao AVE!

unread,
Apr 27, 2023, 12:12:57 PM4/27/23
to mybati...@googlegroups.com
Hello Brian,

EnumTypeHandler is different from the normal type handlers and it requires a special configuration.
The simple rule is:

- Do not specify `typeHanlder`
- Specify `javaType` instead.

So, it should look like this:

RESULT MAP…
<result property="status" column="status" javaType="x.y.UserStatus" />

INSERT…
 #{status,javaType=x.y.UserStatus},

UPDATE…
status = #{status,javaType=x.y.UserStatus},

In some cases, it works with or without `javaType`.
And in other cases, it works even if `typeHandler` is specified, but it should always work if you follow the above rule.

If you have any difficulty, please create a small demo project and share it on your GitHub repository.
Here are some project templates.
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/46cc00c8-d18e-46d7-9291-abc280c6f8d5n%40googlegroups.com.

Brian Hurley

unread,
Apr 27, 2023, 2:12:06 PM4/27/23
to mybatis-user
Thanks Iwao for the response.  However, I've tried what you suggested and still have issues.   I've make a mybatis-issues project ( I didn't know what gh-# to make it so I called it gh-enum). I can change this if you want.



Thanks
Brian

Iwao AVE!

unread,
Apr 27, 2023, 2:31:48 PM4/27/23
to mybati...@googlegroups.com
It was just a few mistakes, it seems.
I have pushed the correction. Please check the commit log.

// Iwao

Brian Hurley

unread,
Apr 27, 2023, 7:01:11 PM4/27/23
to mybati...@googlegroups.com
Thank you that fixed the problem!


Brian






Reply all
Reply to author
Forward
0 new messages