David Martinelli
David Martinelli

Reputation: 233

ORMLite query foreign collection

I searched a lot but I did not understand how to solve my problem. I have two classes like these:

public class A
{
    @DatabaseField(generatedId = true)
    int mId;

    @DatabaseField(canBeNull = false)
    private String mName;

    @ForeignCollectionField(eager = true)
    private Collection<B> mB;
}

public class B
{
    @DatabaseField(canBeNull = false)
    private String mIng;

    @DatabaseField(foreign = true)
    private A mA;
}

My purpose is to fill the object A with the the collection of object B. How can I do? I already have the two DAO object in a class that manage the database.

Upvotes: 1

Views: 724

Answers (2)

Nitesh Tiwari
Nitesh Tiwari

Reputation: 4762

You can proceed this way :

1) Create A using dao of A

2) Using A create B

Code :

A a = new A(mid , mName);

daoA.create(a); 

for (B b : a.mB) {

   b = new B( mIng , a);
   daoB.create(b);
}

Hope this is UseFul...thks

Upvotes: 1

eduyayo
eduyayo

Reputation: 2038

Class B needs a property of type A for it to have a parent key.

After you´re done with that, i code, the easyest way is to save the instance of A and select it again so the foreignCollection is initialized by ORMLite. Then just by calling add(new B()), new child rows of B will be saved.

Upvotes: 0

Related Questions