1

我正在尝试使用 ORMLite 将我的类映射到 SQLite 数据库,但我无法存储 String 的集合我有这个错误:

java.lang.IllegalArgumentException: No fields have a DatabaseField annotation in
     class java.lang.String

这个错误是不言自明的,但是我应该如何存储字符串集合,我应该扩展 String 类并添加所需的注释来做到这一点?在字段的注释中,我添加了 datatype.serializable 但这也行不通。这是我遇到问题的班级:

@DatabaseTable(tableName = "parameters")

public class Parameters {

    // id is generated by the database and set on the object automagically
    @DatabaseField(generatedId = true)
    int id_parameters;

    @DatabaseField(canBeNull = true)
    @JsonProperty("id")
    private Integer id;

    @DatabaseField(canBeNull = false)
    @JsonProperty("account_id")
    private Integer account_id;

    @ForeignCollectionField
    @JsonProperty("languages")
    private Collection<String> languages;
    ...
}

有问题的领域是语言!

4

1 回答 1

4

是的,你不能只做一个字符串的集合。您将需要创建一个包装类,其中包含一个String字段以及一个外部Parameters字段:

public class Language {
    @DatabaseField(generatedId = true)
    int id;
    @DatabaseField
    String language;
    @DatabaseField(foreign = true)
    Parameters parameters;
}

以下是来自 FM的外国收藏品的描述。去引用:

请记住,当您有一个 ForeignCollection 字段时,集合中的类(在此示例中为 Order)必须具有用于具有该集合的类(在此示例中为 Account)的外部字段。如果 Account 有一个国外的 Orders 集合,那么 Order 必须有一个 Account foreign 字段。它是必需的,以便 ORMLite 可以找到与特定帐户匹配的订单。

于 2013-04-24T18:34:29.207 回答