4

My domain class has attributes mapped to an enum. Strangely MyBatis 3.4.x (Both 3.4.0 and 3.4.4. This worked with 3.3.x) with Spring MyBatis 1.3.1 tries to map it with an unrelated enum and gives the error.

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'order_line_programmed' from result set. Cause: java.lang.IllegalArgumentException: No enum constant foo.UnrelatedEnum.yes

My domain class looks like this:

public class OrderLine {

    private Long id;
    private Product product;
    private ProgrammedStatus programmedStatus;
    private String programmedFeedback;
    private boolean completed = false;
}

ProgrammedStatus is a simple enum

public enum ProgrammedStatus {
    yes, no, error;
}

It is this programmedStatus which is mapped to programmed column as follows,

<resultMap id="orderLineResult" type="foo.OrderLine">
    <id property="id" column="technical_order_line_id" />
    <result property="programmedStatus" column="order_line_programmed" typeHandler="org.apache.ibatis.type.EnumTypeHandler" />
    <result property="programmedFeedback" column="order_line_programmed_feedback" />
    <result property="completed" column="order_line_completed"
        javaType="java.lang.Boolean" typeHandler="org.apache.ibatis.type.BooleanTypeHandler" />
    <association property="product"
        notNullColumn="order_line_product_id"
        resultMap="foo.repository.mapper.ProductMapper.productResult" />
</resultMap>

I even tried mapping the javaType with the typeHandler, but MyBatis seem to ignore it.

Few information which may find useful,

  • UnrelatedEnum is also a simple Enum as ProgrammedStatus
  • Product has an attribute which has an attribute which is of type UnrelatedEnum

I found this issue in other places of code as well. I COULD have my own specific typeHandler instead of the EnumTypeHandler here. The issue is that this enum matching is used in a lot of places in my program and migrating wit 3.4 makes my program unstable.

Gunith D
  • 1,843
  • 1
  • 31
  • 36

2 Answers2

5

Removing the explicitly mentioned enum typeHandler worked for me

Remove : typeHandler="org.apache.ibatis.type.EnumTypeHandler"

<resultMap id="orderLineResult" type="foo.OrderLine">
    <id property="id" column="technical_order_line_id" />
    <result property="programmedStatus" column="order_line_programmed" />
    <result property="programmedFeedback" column="order_line_programmed_feedback" />
    <result property="completed" column="order_line_completed"
        javaType="java.lang.Boolean" typeHandler="org.apache.ibatis.type.BooleanTypeHandler" />
    <association property="product"
        notNullColumn="order_line_product_id"
        resultMap="foo.repository.mapper.ProductMapper.productResult" />
</resultMap>
Abhijeet
  • 266
  • 1
  • 3
  • 13
  • It is so odd that the information can be found only in SO answer with the single upvote. It definitely should be represented in the official Mybatis migration guide from v.3.4 to v.3.5 – kirill.login Apr 22 '21 at 05:47
0

As explained here https://github.com/mybatis/mybatis-3/issues/995 - EnumTypeHandler shouldn't be specified explicitly as a type handler for enum properties because (as a type handler) it gets cached and re-used in all other mappings where specified explicitly (which leads to the original issue). To avoid this, let MyBatis figure out the type handler for enum properties. In case your enum is 'special', you can subclass EnumTypeHandler and explicitly reference it where needed, but then caching is OK, because it is handling the exact same enum type every time.

andrey
  • 23
  • 4