user1811841
user1811841

Reputation: 21

Unable to find a usable constructor for class Spring Enum Mybatis exception

I am trying to setup ENUM TypeHandlers in spring mybatis, getting below exception,

Cause: org.apache.ibatis.type.TypeException: Unable to find a usable constructor for class com.citi.aw.data.ibatis.KnownTypeHandlers$PortfolioTypeEnum

SqlConfig.xml

<configuration>
 <typeHandlers>
    <typeHandler javaType="com.citi.aw.entities.portfolio.PortfolioType" 
        handler="com.citi.aw.data.ibatis.KnownTypeHandlers$PortfolioTypeEnum"/></typeHandlers>
</configuration>

PortfolioType Enum

public enum PortfolioType
{
    PROPOSAL,
    INVESTABLE,
    MODEL,
    MODEL_STRATEGY,
    STRATEGY,
    CLIENT,
    FUND_OF_FUND,
    REFERENCE,
    MODEL_ALLOCATION    
}

Class KnownTypeHandlers
 public class KnownTypeHandlers 
    {
        public static abstract class PortfolioTypeEnum extends OrdinalEnumTypeHandler
        {
            public PortfolioTypeEnum()
            {
                super(PortfolioType.values());
            }
        }
}

Class OrdinalEnumTypeHandler

    public abstract class OrdinalEnumTypeHandler extends org.apache.ibatis.type.BaseTypeHandler<Object>
{
    private Object[] enumValues;

    protected OrdinalEnumTypeHandler(Object[] enumValues)
    {
        this.enumValues = enumValues;
    }

    @Override
    public Object getResult(ResultSet rs, String columnName) throws SQLException
    {
        int intValue = rs.getInt(columnName);
        return enumValues[intValue];
    }

    @Override
    public Object getResult(ResultSet rs, int columnPos) throws SQLException
    {
        int intValue = rs.getInt(columnPos);
        return enumValues[intValue];
    }

    @Override
    public Object getResult(CallableStatement arg0, int arg1) throws SQLException
    {
        throw new SQLException("not implemented");
    }

    @SuppressWarnings("unchecked")
    //@Override
    public void setParameter(PreparedStatement ps, int pos, Object parameter, String jdbcType) throws SQLException
    {
        ps.setInt(pos, ((Enum)parameter).ordinal());
    }

    //@Override
    public Object valueOf(String stringValue)
    {
        return enumValues[Integer.parseInt(stringValue)];
    }
}

Upvotes: 1

Views: 3895

Answers (1)

Pau
Pau

Reputation: 16116

I guess you are using MyBatis 3. So, You don't need to create any handler for the enum. MyBatis has one typehandler to Enum cases.

Se the MyBatis 3 Configuration in the reference documentation:

typeHandlers

Whenever MyBatis sets a parameter on a PreparedStatement or retrieves a value from a ResultSet, a TypeHandler is used to retrieve the value in a means appropriate to the Java type. The following table describes the default TypeHandlers.

...

EnumTypeHandler - Enumeration Type - VARCHAR any string compatible type, as the code is stored (not index).

Then add to your configuration xml (In your case SqlConfig.xml:

<!-- mybatis-config.xml -->
<typeHandlers>
  <typeHandler handler="org.apache.ibatis.type.EnumTypeHandler"/>
</typeHandlers>

Upvotes: 3

Related Questions