ORMLite 不知道如何存储接口java.util.List

ORMLite does not know how to store interface java.util.List

产品

@DatabaseTable
public class Product implements Serializable {
private static final long serialVersionUID = 1L;

public Product() {
    // TODO Auto-generated constructor stub
}

public Product(String originalId) {
    this.originalId = originalId;
}

@DatabaseField(id = true)
private String originalId;

...

@DatabaseField
private List<SkuInfo> skusInfo;

SkuInfo

public class SkuInfo implements Serializable {

private static final long serialVersionUID = 1L;
...
}

错误是:

java.lang.IllegalArgumentException: ORMLite does not know how to store interface java.util.List for field skusInfo. Use another class or a custom persister.

当我使用:

@DatabaseField(dataType = DataType.SERIALIZABLE)
private List<SkuInfo> skusInfo;

错误更改为:

java.lang.IllegalArgumentException: ORMLite does not know how to store interface java.util.List for field skusInfo. Use another class or a custom persister.

我已经在另一篇文章中读到关于标签 Foreign 的内容,但我的项目不是外键。

我使用了自定义可序列化对象,就像发布的那样 here 并且工作了。

public class SerializableCollectionsType extends SerializableType {
    private static SerializableCollectionsType singleton;
    public SerializableCollectionsType() {
        super(SqlType.SERIALIZABLE, new Class<?>[0]);
    }

    public static SerializableCollectionsType getSingleton() {
        if (singleton == null) {
            singleton = new SerializableCollectionsType();
        }
        return singleton;
    }
    @Override
    public boolean isValidForField(Field field) {
        return Collection.class.isAssignableFrom(field.getType());
    }
}

产品

@DatabaseField(persisterClass = SerializableCollectionsType.class)
private List<SkuInfo> skusInfo;

java.lang.IllegalArgumentException: ORMLite does not know how to store interface java.util.List for field skusInfo. Use another class or a custom persister.

只是为了后代,您可以将其更改为 ArrayList,它实际上实现了 java.io.Serializable,因为这就是它正在寻找的。不幸的是 List 没有,所以 ORMLite 抛出异常。