Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One-To-Many relationship in ORMLite Android

How do I implement one-many relationship in 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);

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?

like image 361
Rockin Avatar asked Jul 03 '13 07:07

Rockin


1 Answers

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?

Right. You need to create the A items using:

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

ORMLite does not automatically create those for you.

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

like image 96
Gray Avatar answered Nov 04 '22 20:11

Gray