How to map Enum in GreenDAO
android, enums, greendao, orm
Solution
As far as I know, Enums are not supported by greenDAO due to their unstable nature. Also they are an error-prone component to add to your database logic, since the values of the enum elements can change.
One option to get around this would be to add an Int property to the database and then map Enum ordinal values to that field, like so:
// add the int property to the entity
unicorn.addIntProperty("shirtSize");
// create the enum with static values
public enum ShirtSize {
XS(1), S(2), M(3), L(4), XL(5), XXL(6);
private final int value;
private ShirtSize(int value) {
this.value = value;
}
public int value() {
return value;
}
}
// set the ordinal value of the enum
weather.setShirtSize(ShirtSize.XL.value());
Problem
I've just started using greenDAO. How do I add an Enum property? What I've Thought of: using the addIndex property of an entity. ``` private static void main() { // TODO Auto-generated method stub static Schema blah; Entity unicorn = blah.addEntity("Weather"); unicorn.addIdProperty(); unicorn.addIntProperty("currentAirTemp"); unicorn.addIndex("shirtSize"); } ``` Is this the right way to do it? Aim: I want to have a reference to shirtSize being from the set: {XS, S, M, L, XL, XXL}