0

I have the following:

public class Stat {

    public enum HitType {
        MOBILE1(0), MOBILE2(1), DESKTOP(2);
        public final int value;
        public int value() { return value; }
        HitType(int val) {
            value = val;
        }
        public static HitType parseInt(int i) {
            switch (i) {
                case 0: return MOBILE1;
                case 1: return MOBILE2;
                case 2: return DESKTOP;
                default: return null;
            }
        }
    }

    public HitType hitType;
    public long sourceId;

    public Stat(... int hitType, BigInteger sourceId) {
        this.hitType = HitType.parseInt(hitType);
        this.sourceId = sourceId.longValueExact();

@Mapper
public interface StatMapper {

    @Select("select * from stats where id = #{id}")
    @Results(value = {
        @Result(property = "hitType", column = "hit_type"),
        ...
        })
    public Stat findById(@Param("id") long id);

    Stat s = statMapper.findById(1);
    response.getOutputStream().print(s.toString());

It still gives this error:

Resolved exception caused by Handler execution: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'hit_type' from result set. Cause: java.lang.IllegalArgumentException: No enum constant com.company.app.model.Stat.HitType.2

I tried http://stackoverflow.com/questions/5878952/ddg#5878986 and read Convert integer value to matching Java Enum.

If I change the constructor signature to

public Stat(..., int hitType, long sourceId) {
    this.sourceId = sourceId;

Then it gives the error

nested exception is org.apache.ibatis.executor.ExecutorException: No constructor found in com.company.app.model.Stat matching [java.math.BigInteger, java.lang.String, java.sql.Timestamp, java.lang.Integer, java.math.BigInteger]

So it seems in the first case it may be setting properties directly, while in the 2nd case, it is using the constructor.

I tried putting HitType hitType in the constructor signature, but it still gives me the No constructor found... error.

MyBatis 3.4.5, MyBatis-Spring 1.3.1, Spring-Boot 1.5.13

Chloe
  • 25,162
  • 40
  • 190
  • 357

1 Answers1

0

I added the getter & setter for that type

public HitType getHitType() {
    return hitType;
}

public void setHitType(int hitType) {
    this.hitType = HitType.parseInt(hitType);
}

And then it started working. Which is weird because it was complaining about the constructor signature. If it is using the constructor, why would it need the getters and setters?

Chloe
  • 25,162
  • 40
  • 190
  • 357