ForeignCollection: no update possible

41 views
Skip to first unread message

Thomas Keller

unread,
Jun 24, 2014, 10:17:00 AM6/24/14
to ormlite...@googlegroups.com

Hi!

I've built a small object tree consisting of three models A, B and C, where A:B is 1:n and B:C is 1:n as well. Now when I save such a tree to a (FK-enabled) SQLite database, I usually do:

dao.createOrUpdate(instanceOfA);
for (B instanceOfB : someBList) {
    instanceOfA.getBs().add(instanceOfB);
    for (C instanceOfC : someCList) {
        instanceOfB.getCs().add(instanceOfC);
    }
}

That works all well, except that when I really want to update the tree, the B and C add()'s bail out with a duplicate PK exception. (Note here that neither the BList in A nor the CLists in the Bs are populated from the database; the instanceOfA I serialize here comes from the network.)

Now I believe this bites me because `BaseForeignCollection.addElement()` uses `dao.create(<element>)` instead of `dao.createOrUpdate(<element>)`.

Is this behaviour by design? Should I rather load a matching instanceOfA from database at first and then add the B's and C's or do I even have to manually match them for existance in the foreign collection?

Thomas.

Reply all
Reply to author
Forward
0 new messages