我正在尝试使用 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/