orlite Android中的一对多关系

时间:2022-01-21 06:48:20

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。有谁能帮忙吗?

3 个解决方案

#1


14  

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 A items 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.

您可以查看涉外集合示例程序的源代码。

#2


0  

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中的集合。

#3


-1  

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

#1


14  

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 A items 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.

您可以查看涉外集合示例程序的源代码。

#2


0  

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中的集合。

#3


-1  

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