【问题标题】:Ormlite ForeignCollection with GsonOrmlite ForeignCollection 与 Gson
【发布时间】:2014-07-08 01:56:07
【问题描述】:

整天都在为此苦苦挣扎。我觉得我离正确的解决方案只有一个注释。

我从 API 获取 JSON,并在 Volley 请求中使用 Gson 将其解析为对象。 然后我想使用 ORMLite 将对象存储在 DB 中。

问题是我的 JSON 有其他对象的列表。所以我决定需要 ForeignCollection 。

这是我得到的 JSON 的简化版本:

{
    "b": [
        {"title"="abc","id="24sfs"},
        {"title"="def", "id="532df"}
    ],
    "c": [
        {"description"="abc","id="34325"},
        {"description"="def", "id="34321"}
    ],
    "id"="ejsa"
}

让我们将整个对象称为A类。“b”内的对象是B,“c”内的对象是C类。

B 和 C 是相似的。这导致以下类定义:

class A {

    @DatabaseField(index = true, unique = true, id = true)
    private String id;
    @ForeignCollectionField(eager = true)
    public Collection<B> bCollection;
    public  ArrayList<B> b;
    @ForeignCollectionField(eager = true)
    public Collection<C> cCollection;
    public ArrayList<C> c;
}

class B  {
    @DatabaseField(foreign=true)
    public A a;
    @DatabaseField(id = true, index = true, unique = true)
    public String id;
    @DatabaseField
    public String title;
}

我们需要 ArrayList b 和 c 的原因是 gson 可以正确解析它。因此,一旦我在内存中有 A 类,这就是我要存储它的方法

private void storeA(A a) {
    if (a.b != null) {
        getHelper().getDao(B.class).callBatchTasks(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                for (B b : a.b) {
                    b.a = a;
                    try {
                        getHelper().getDao(B.class).createOrUpdate(b);
                    } catch (Exception e) {
                    }
                }
                return null;
            }
        });
    }

    /*
    Here we start running into problems. I need to move the data from the ArrayList to the Collection
    */
    a.bCollection = a.b; // but this seems to work, since bCollection is a Collection
    a.cCollection = a.c;
    getHelper().getDao(A.class).createOrUpdate(a);
}

所以它似乎存储正确,据我所知没有错误。但是当我尝试如下检索时,我无法从 bCollection 中检索任何内容:

private void load() {
    try {
        List<A> as = getHelper().getDao(A.class).queryForEq("id", "ejsa");
        if (as != null && as.size() > 0) {
            A a = as.get(0);
            CloseableWrappedIterable<B> cwi = a.bCollection.getWrappedIterable();

            try {
                for (B b : cwi) {
                    Log.e(b.title);
                }
            } finally {
                cwi.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我做错了什么?我需要为其中一些内容指定 foreignColumnName 吗?我不知道这些东西是没有正确存储还是我没有正确检索它们?

【问题讨论】:

    标签: android gson ormlite foreign-collection


    【解决方案1】:

    我会尝试删除以下两行:

    a.bCollection = a.b;
    a.cCollection = a.c;
    

    当您查询 A 时,ORMLite 应该会自动为您填充 A 的 ForeignCollection,您不需要自己设置它们。

    【讨论】:

    • 好的,我删除了它,但我仍然无法判断是否正在保存内容,因为我无法检索它。一旦我有一个 A 对象,我如何从中得到一个 B 的列表?
    • 我希望上面的 load() 函数能够正常工作...为了验证内容是否正确保存到数据库中,我建议将 sqlite 文件从模拟器中拉出:“adb pull /data/ data/your.package.name/databases/yourdatabasefilename" 然后您可以使用sourceforge.net/projects/sqlitebrowser 之类的工具来验证您看到的 B 表中填充了正确的外部 id 字段。
    • 是的,我认为我的保存功能有误。又改了几次,现在它就像一个魅力。
    猜你喜欢
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多