如何在GreenDAO中映射Enum

7

我刚开始使用greenDAO。

如何添加一个枚举属性?

我的想法:使用实体的addIndex属性。

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");
}

这样做是正确的吗?

目标:我想要一个参考,表明shirtSize来自集合:{XS,S,M,L,XL,XXL}

3个回答

22

在使用GreenDAO 3时,我们现在可以选择使用@convert注释与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();
        }
    }
}

阅读更多请访问http://greenrobot.org/objectbox/documentation/custom-types/


问候,但超链接似乎无法链接到代码。 - Ninja
@Ninja,你说得对。看起来它已经被移动到http://greenrobot.org/objectbox/documentation/custom-types/,并对示例进行了一些调整。我会更新答案。 - Marcus

7

最新版本的GreenDao (2.x) 包含了非常适合您需求的功能。它提供了一些自定义类型,可以轻松处理枚举。

枚举

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;
    }
}

转换器

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.addIntProperty("ShirtSize").customType(
    "com.your_package.ShirtSize",
    "com.your_package.ShirtSizeConverter"
);

1
ShirtSize.values() 使用序数值而不是建议的稳定 ID 概念。 - drindt

2
据我所知,由于其不稳定性,greenDAO不支持枚举。 此外,将枚举元素的值更改为一个错误易发组件也会加入到您的数据库逻辑中。
解决此问题的一种方法是向数据库添加Int属性,然后将枚举序数值映射到该字段,如下所示:
// 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());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接