Pankaj Sharma
Pankaj Sharma

Reputation: 679

Not able to map db entries to java enum

I'm getting following error while fetching entries from DB using mybatis (v3.1.1) in a Spring app.

It's related to mapping of string(used to save value in db) to enum (used for mapping).

Stack trace is as follow:

List com.expertly.service.TransactionLogServiceImpl.getTransactionLogs(ActionType) threw org.apache.ibatis.exceptions.PersistenceException: 
    ### Error querying database.  Cause: java.lang.IllegalArgumentException: No enum const class com.expertly.common.model.reports.TransactionState.COMMITTED 
    ### The error may exist in com/expertly/data/mapper/TransactionLogMapper.java (best guess)
    ### The error may involve com.expertly.data.mapper.TransactionLogMapper.getTransactionLogs-Inline
    ### The error occurred while setting parameters
    ### SQL: SELECT id, transactionstate AS "transactionState", sessionid AS "userSession.sessionId" FROM transactionlog WHERE actioncode = 'VIEW_USER'

Caused by: java.lang.IllegalArgumentException: No enum const class com.expertly.common.model.reports.TransactionState.COMMITTED 
    at java.lang.Enum.valueOf(Enum.java:196) ~[na:1.6.0_38]
    at org.apache.ibatis.type.EnumTypeHandler.getNullableResult(EnumTypeHandler.java:43) ~[mybatis-3.1.1.jar:3.1.1]
    at org.apache.ibatis.type.EnumTypeHandler.getNullableResult(EnumTypeHandler.java:23) ~[mybatis-3.1.1.jar:3.1.1]

My mapper's function:

@Select("SELECT id, transactionstate AS \"transactionState\", sessionid AS \"userSession.sessionId\" "+
    "FROM transactionlog WHERE actioncode = #{actionType} ")
List<TransactionLog> getTransactionLogs(@Param("actionType") ActionType actionType) throws TransactionLogMapperException;

TrsnacationLog class has following members:

private int id;
private UserSession userSession;
private ActionType actionType;
private TransactionState transactionState;
private String paramValue;
private Timestamp startTime;
private Timestamp endTime;

The ActionType enum is as follow:

package com.expertly.common.model.reports;

import java.io.Serializable;

import com.google.gwt.user.client.rpc.IsSerializable;

public enum TransactionState implements Serializable, IsSerializable{
    STARTED("STARTED"),
    COMMITTED("COMMITTED"),
    FAILED("FAILED");

    private final String state;

    TransactionState(String state){
        this.state = state;
    }

    @Override
    public String toString(){
        return state;
    }
}

Please let me know what's wrong and how could I fix this.

Upvotes: 1

Views: 2040

Answers (1)

hemantvsn
hemantvsn

Reputation: 1446

Please check whether you have WHITE SPACES into the string you are getting from DB. That should be the only discrepancy as don't see any typos.

Upvotes: 2

Related Questions