android - 如何将原始类型(String)的集合存储到数据库中

标签 android android-sqlite ormlite primitive-types foreign-collection

我正在尝试使用 ORMLite 将我的类映射到 SQLite 数据库,但我在存储字符串集合时遇到了问题我有这个错误:

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

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

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

有问题的领域是语言!

最佳答案

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

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

这是 description of foreign collections从调频。引用:

Remember that when you have a ForeignCollection field, the class in the collection must (in this example Order) must have a foreign field for the class that has the collection (in this example Account). If Account has a foreign collection of Orders, then Order must have an Account foreign field. It is required so ORMLite can find the orders that match a particular account.

关于android - 如何将原始类型(String)的集合存储到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16190195/

相关文章:

android - 如何在 web 模式下将 flutter_web_view 转换为 HtmlElementView

android - Android 中的 Sqlite 查询自动递增和默认值

android - 从 SQLite 数据库加载 10000 行最快的方法是什么?

java - SQLite 异常 : table already exists

android - 使用 Proguard 后无法让 Ormlite 工作

java - 如何更改OrmLite的编码?

Android:C2DM 在没有通知的情况下更改 registration_id?

java - 在android中更快地下载给定url的图像

android - GPS信号采集超时怎么办

java - OrmLite 多对多的扭曲