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}

有帮助吗?

解决方案 3

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());

其他提示

Using GreenDAO 3 we now have the option to use @convert annotation with PropertyConverter

@Entity
public class User {
    @Id
    private Long id;

    @Convert(converter = RoleConverter.class, columnType = String.class)
    private Role role;

    enum Role {
        DEFAULT, AUTHOR, ADMIN
    }

    static class RoleConverter implements PropertyConverter<Role, String> {
        @Override
        public Role convertToEntityProperty(String databaseValue) {
            return Role.valueOf(databaseValue);
        }

        @Override
        public String convertToDatabaseValue(Role entityProperty) {
            return entityProperty.name();
        }
    }
}

Read more at http://greenrobot.org/objectbox/documentation/custom-types/

Latest version of GreenDao (2.x) contains functionality which ideally suits your needs. There are a Custom Types which can serve enums very easily.

Enum

public enum ShirtSize {
    XS(1),
    S(2),
    M(3),
    L(4),
    XL(5),
    XXL(6);

    private final int value;

    ShirtSize(int value) {
        this.value = value;
    }

    public int value() {
        return value;
    }
}

Converter

public class ShirtSizeConverter implements PropertyConverter<ShirtSize, Integer> {
@Override
public ShirtSize convertToEntityProperty(Integer databaseValue) {
    if(databaseValue == null) {
        return null;
    } else {
        for(ShirtSize value : ShirtSize.values()) {
            if(value.value() == databaseValue) {
                return value;
            }
        }

        throw new DaoException("Can't convert ShirtSize from database value: " + databaseValue.toString());
    }
}

@Override
public Integer convertToDatabaseValue(ShirtSize entityProperty) {
    if(entityProperty == null) {
        return null;
    } else {
        return entityProperty.value();
    }
}

}

Entity field declaration (in generator)

entity.addIntProperty("ShirtSize").customType(
    "com.your_package.ShirtSize",
    "com.your_package.ShirtSizeConverter"
);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top