将对象列表保存到ORMLite数据库

将对象列表保存到ORMLite数据库

本文介绍了将对象列表保存到ORMLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用ORMLite将对象列表保存到数据库的方法并阅读这个问题:

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

我唯一不理解的是这行私人收藏< MyString> bunchOfStrings = new ArrayList< MyString>()。为什么我们将 ForeignCollectionField 保存为 Collection< MyString> 而不是 ArrayList< MyString> ?使用上面的 bunchOfStrings 对象时,我们是否总是需要将其强制转换为 ArrayList< MyString>

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



你不必初始化那个字段,Ormlite会这样做。因此,只有可用的方法是Collection或ForeignCollection

You dont have to initialize that field, Ormlite will do that. Hence, only available methods are ones present in Collection or ForeignCollection

这篇关于将对象列表保存到ORMLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:20