MyBatis does not resolve Inner class when parsing SQL Mapper Configuration handlers

Jugu picture Jugu · Mar 4, 2015 · Viewed 8.4k times · Source

Using MyBatis 3.2.8, I'm trying to map an enum type (Status) to the jdbc VARCHAR type (to can use only the enum in my entity bean). So I defined the TypeHandler UserStatusHandler

import com.sample.User.Status;
import org.apache.ibatis.type.EnumTypeHandler;

public class UserStatusHandler extends EnumTypeHandler<Status>
{
  public UserStatusHandler(Class<Status> type)
  {
    super(type);
  }
}

I correctly declared the handler in the xml config file and in the UserDao.xml (mapping the attribute Status to the VARCHAR in the resultMap ...) sample :

In the XML config file:
<typeHandlers>
    <typeHandler handler="com.sample.dao.UserStatusHandler" javaType="com.sample.User.Status"/>
</typeHandlers>

In the DAO mapper XML file:
<resultMap id="UserResultMap" type="User">
  <id property="id" column="ID" javaType="long"/>
  <result property="status" column="STATUS" typeHandler="com.sample.dao.UserStatusHandler" javaType="com.sample.User.Status"/>
  xxxxx
</resultMap>

But the problem incoming from MyBatis was that MyBatis cannot found my java enum class because it is defined inside another interface

public interface User
{
  public enum Status
  {
    A, B, C
  }
  ...
}

When I define this enum class in a separate file, it works with no problem, but I don't like to change my design (because of a limit ? ), I search to understand why MyBatis cannot found the class in this case ? Is it a way to fix this ?

MyBatis cannot build the SqlSession. While executing a simple test to find a User I get the following exception

Cause: org.apache.ibatis.builder.BuilderException: 
Error parsing SQL Mapper Configuration. Cause:  
org.apache.ibatis.builder.BuilderException: Error resolving class. 
Cause:     org.apache.ibatis.type.TypeException: Could not resolve type 
alias 'com.sample.User.Status'.  
Cause: java.lang.ClassNotFoundException: Cannot find class: 
com.sample.User.Status

Answer

Jugu picture Jugu · Mar 6, 2015

Finally I solved it by writing, in the XML configuration file, the name of the Inner enum like its associated compiled file name in the jar i.e. by adding a dollar $ between the enveloped and the inner class.

com.sample.User$Status

It appears a bug or a limit in MyBatis ..