简体   繁体   中英

Empty column values mapping to java enum using ORMlite

How do I map empty or unknown values to the enum in java. I am using Ormlite.

@DatabaseField(canBeNull=false, columnName=COLUMN_NAME_DEVICE_TYPE,  
     dataType=DataType.ENUM_STRING)
private DeviceType deviceType;

And here is my enum

public enum DeviceType {

    iPhone, iPad, Android,
    OSX, Windows, Windows8,
    WP7, WP8, Blackberry,
    Browser, Facebook, Unknown;

    @JsonCreator
    public static DeviceType parse(String s) {
        for (DeviceType t : values()) {
            if (t.name().toLowerCase().equals(s)) {
                return t;
            }
        }
        return Unknown;
    }

    @JsonValue
    @Override
    public String toString() {
        return name().toLowerCase();
    }
}

Am always getting the following exception:

Caused by: java.sql.SQLException: Cannot get enum value of '' for field FieldType:name=deviceType,class=MyEntity

Take a look on data in database. I guess that this row has empty string '' for which no enum exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM