如何将通用枚举作为 OrmLite 列

How to make Generic Enum as OrmLite Column

我正试图通过 "generifying" 我的查找表来尽量减少我将要做的一些工作。他们都将 int id 作为主键,但我也希望他们能够通过 "type" (无论枚举值是什么)进行搜索。这是抽象的 LookupRecord class

public abstract class LookupRecord<T extends Enum<T>> extends DatabaseRecord {
    public static final String TYPE_FIELD_NAME = "type";

    @DatabaseField(unique = true, columnName = TYPE_FIELD_NAME)
    private T type;

    public LookupRecord(T type) {
        this.type = type;
    }

    public T getType() {
        return type;
    }
}

因此,每当我尝试为任何查找表初始化 Dao 时,我都会收到以下错误:

java.sql.SQLException: Field FieldType:name=type,class=LookupRecord improperly configured as type com.j256.ormlite.field.types.EnumStringType@5e80d5

They are all going to have int id as primary key but I'd also like them to have the ability to be searched by their "type" (whatever the Enum value is).

@DatabaseField(unique = true, columnName = TYPE_FIELD_NAME)
private T type;

是的,这是行不通的,因为 type erasure. When ORMLite 使用反射来调查有问题的 class,它会尝试查找该字段的枚举常量。它得到的只是类型 Enum 而不是来自子 class 的特定具体枚举 class。不幸的是,该错误消息对指出这一点没有帮助。

配置 class 时,它只知道它是一个枚举。它不知道 它是哪个 枚举,因此它无法正确配置该字段或创建具有适当枚举字段的对象实例。