java ORMLite Android 中的一对多关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17441694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:59:52  来源:igfitidea点击:

One-To-Many relationship in ORMLite Android

javaandroidone-to-manyormlite

提问by Rockin

How do I implement one-many relationship in ORMLite Android?

如何在 ORMLite Android 中实现一对多关系?

please find the example

请找出例子

public class A {
 private String name;
    @DatabaseField (foreign = true, foreignAutoRefresh = true, columnName = "A")
    private B b;
    @DatabaseField(columnName = "author")
    private String authorName;
}

public class B {
    @DatabaseField(generatedId = true, columnName = "id")
    private long id;
    @DatabaseField(columnName = "name")
    private String name;
    @ForeignCollectionField
    Collection<A> aees;
}

B has collection of A. I am calling dao.create(b);

B 有 A 的集合。我正在打电话 dao.create(b);

Now i create dao of b, since b has all the data. But the table B has only created with data, A is never created. Kindly could some one help?

现在我创建了 b 的 dao,因为 b 拥有所有数据。但是表 B 只创建了数据,A 从未创建过。请有人帮忙吗?

回答by Gray

Now i create dao of b, since b has all the data. But the table B has only created with data, A is never created. Kindly could some one help?

现在我创建了 b 的 dao,因为 b 拥有所有数据。但是表 B 只创建了数据,A 从未创建过。请有人帮忙吗?

Right. You need to create the Aitems using:

对。您需要A使用以下方法创建项目:

for (A a : b.aees) {
   aDao.create(a);
}

ORMLite does not automatically create those for you.

ORMLite 不会自动为您创建那些。

You might take a look a the source code of the foreign-collection example program.

你可以看一下foreign-collection示例程序的源代码。

回答by bogdan

You have to override the DAO of the B class, so when an object B is created or updated, the objects in the collection to be also updated.

你必须重写 B 类的 DAO,所以当一个对象 B 被创建或更新时,集合中的对象也被更新。

Take a look at this question: Collections in ORMLite.

看看这个问题:ORMLite 中的集合

回答by Yasir Ali

i was facing the same problem. My json was like:

我面临同样的问题。我的 json 是这样的:

{
   "parent":{
            "name":"ABC",
            "children":[
                          {"childName":"1"},
                          {"childName":"2"},
                          {"childName":"3"}
                       ]
             }
}

i resolved the issue like this:

我解决了这样的问题:

Parent parent = new Parent();
parent.setName("ABC");

while(get children one by one from json data)
{
    childrenArray.add(new Child(its Name));
}

parentDAO.create(parent);

for(Child child : childrenArray)
{
    child.setParent(parent);
    childDAO.create(child);
}