如何在 ormlite 中将 enum const 作为接口类型持久化?

How to persist enum const in ormlite as a interface type?

我有一个枚举,它实现了一个接口。这个接口的目的只是在多个枚举之间创建一个绑定,以便我以后可以实现一个插件系统。 (可能会把事情弄清楚一点。)

我的图书馆枚举是这样的:

public interface Resource extends Displayable<Resource> {

    // all the methods that implements my enum
    // Displayable is just an interface that I need in my game
    // Just doesn't consider it, it can't interfer
}

以及实现此接口的枚举示例:

public enum LibraryEnum implements Resource {
    // static final fields

    // fields and resources

    // all implemented methods
}

问题是我想将它存储为一个接口,以启用一种枚举继承,因为我将要创建一个插件系统。任何开发人员都必须实现这个独特的接口才能在游戏中添加资源。但是我不知道他(或她)会怎么命名(可能会有一些双打)。

我有一个带有关联字段的 class,如下所示:

@DatabaseTable(tableName = "packs")
public class Pack implements Displayable<Pack> {

    @DatabaseField(columnName = "id", generatedId = true)
    private Long packId;

    // Here it is
    @DatabaseField
    private Resource resource;
    // Is there any annotation arguments to add ?

    @DatabaseField
    private int quantity;

    // Some other fiels
    // Then constructors and methods
}

我稍微阅读了文档,它说要创建 DataPersister 接口的自定义实现。所以我开始做,但是要实现的方法太多了(20?),我不知道从哪里开始,从哪里结束。也许这是错误的方法? 该文档显示了一个示例,其中方法甚至不存在,并且对于java(日期和日期时间)中已经存在的类型。

我怎样才能做到这一点?或者这甚至可能吗?如果没有,有没有办法做我想做的事(在数据库中存储未知的常量枚举字段)?

I've read the docs a little bit, and it say to create a custom implementation of the DataPersister interface. So I began to do it, but there is so many methods to implement (20?), that I don't know where to began and where to end.

首先你应该考虑 RTFM on custom persisters. I've spent a good bit of time on the ORMLite 文档。

正确的做法是从 ORMLite 扩展当前实现的持久性。例如,您可以扩展 BaseEnumType if you are persisting an enum. If none of the persisters work then you should extend the generic BaseDataType

使用BaseDataType,您需要实现的是:

public Object parseDefaultString(FieldType fieldType, String defaultStr);

public Object resultToSqlArg(FieldType fieldType, DatabaseResults results, int columnPos);

虽然不是必需的,但您可能还想覆盖:

public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos)

通常您会希望重写其他几个方法来调整自定义持久化器的行为。