Martin Heywang
Martin Heywang

Reputation: 27

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

I have one enum, which implements an interface. The purpose of this interface is only to create a bound between multiples enums, so that I can later implements a plugin system. (This post may clear the thing a little bit.)

My library enum lloks like this :

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
}

And an exemple of an enum that implements this interface :

public enum LibraryEnum implements Resource {
    // static final fields

    // fields and resources

    // all implemented methods
}

The thing is that I want to store this as an interface, to enable a sort of enum inheritance, because I'm about to create a plugin system. Any dev would just have to implements this unique interface to add resources in the game. But I don't know how he (or she) would name it (there might be some doubles).

I have a class with the associated field, as shown here :

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

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. Maybe it's the wrong way to do it? The docs shows an exemple where are the methods aren't even there, and for the type that already exists in java (Date & DateTime).

How can I achieve this? Or is this even possible? If not, is there anyway to do what I want anyway (store unknown const enum fields in the database)?

Upvotes: 2

Views: 116

Answers (1)

Gray
Gray

Reputation: 116908

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.

First off you should consider RTFM on custom persisters. I've spent a good bit of time on the ORMLite docs.

The right thing to do is to extend a currently implemented persister from ORMLite. For example, you could extend the BaseEnumType if you are persisting an enum. If none of the persisters work then you should extend the generic BaseDataType.

With BaseDataType, all you need to implement is:

public Object parseDefaultString(FieldType fieldType, String defaultStr);

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

Although not required, you probably also want to override:

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

Typically you'd want to override a couple other methods to tweak the behavior of your custom persister.

Upvotes: 1

Related Questions