问题描述
我正在寻找一种使用 ORMLite 将对象列表保存到数据库的方法并阅读此问题:存储多个 ArrayList 的最佳方法在应用程序设置的 ORMLite 中
接受的答案对我来说很有意义:
public class YourClass {@GeneratedId私有整数 ID;@ForeignCollectionField私人收藏bundleOfStrings = new ArrayList();}公共类 MyString{@DatabaseField(canBeNull = true,foreign = true)私人 YourClass yourClass;@数据库字段私人字符串文本;}
而我唯一不明白的是这一行 private CollectionbundleOfStrings = new ArrayList()
.为什么我们将 ForeignCollectionField
保存为 Collection
而不是 ArrayList
?当使用上面的 bunchOfStrings
对象时,我们是否总是需要将它转换为 ArrayList
?
那是设计考虑,摘自 doc一个>
订单的字段类型必须是 ForeignCollection;或集合- 没有其他支持集合是因为它们更重,有很多方法可以支持
当使用上面的bundleOfStrings对象时,我们是否总是需要将其转换为 ArrayList
您不必初始化该字段,Ormlite 会这样做.因此,只有存在于 Collection 或 ForeignCollection 中的方法才可用
I am looking for a way to save a list of objects to the database with ORMLite and read upon this question: Best way to store several ArrayLists in ORMLite for ApplicationSettings
And the accepted answer makes sense to me:
public class YourClass {
@GeneratedId
private int id;
@ForeignCollectionField
private Collection<MyString> bunchOfStrings = new ArrayList<MyString>();
}
public class MyString{
@DatabaseField(canBeNull = true, foreign = true)
private YourClass yourClass;
@DatabaseField
private String text;
}
And the only thing that I don't understand is this line private Collection<MyString> bunchOfStrings = new ArrayList<MyString>()
. Why do we save the ForeignCollectionField
as Collection<MyString>
instead of as ArrayList<MyString>
? When working with the bunchOfStrings
object above, do we always need to cast it to ArrayList<MyString>
?
That was design consideration, excerpt from doc
The field type of orders must be either ForeignCollection<T> or Collection<T> – no other
collections are supported because they are much heavier with many methods to support
You dont have to initialize that field, Ormlite will do that. Hence, only available methods are ones present in Collection or ForeignCollection
这篇关于将对象列表保存到 ORMLite 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!