0

Is there a ORM library on Android allowing inserting POJO containing foreign field/collection? Because those I found (ORMLite for Android) usually works like that:

  1. Insert parent
  2. Link a child field to the parent object
  3. Insert the children
  4. Update the parent to make him include those foreign field

I found my code ugly having to seek for all the inner child. I would like to only have to insert the parent and then the child would be added automatically, is that even possible?

This is what I do now to insert parent full of foreign field not yet inserted:

    CategoriesDAO.getInstance().addOrUpdate(categories);

    for (EntityCategories.EntityCategory currentCategory : new ArrayList<>(categories.getCategories())) {

        if (currentCategory.getmPlans() != null) {
            for (EntityPlan myPlan : new ArrayList<>(currentCategory.getmPlans())) {
                EntityPlan oldPlan = PlanDAO.getInstance().queryById(String.valueOf(myPlan.getmId()));
                myPlan.setCategories(currentCategory);
                if (oldPlan != null) {
                    if (!myPlan.getmDateModification().equals(oldPlan.getmDateModification())) {
                        PlanDAO.getInstance().addOrUpdate(myPlan);
                    }
                } else {
                    PlanDAO.getInstance().addOrUpdate(myPlan);
                }
            }

        } else {
            continue;
        }

        LabelDAO.getInstance().addOrUpdate(currentCategory.getmLabel());
        currentCategory.setCategories(categories);
        CategoryDAO.getInstance().addOrUpdate(currentCategory);
    }
Jaythaking
  • 2,200
  • 4
  • 25
  • 67
  • Try `Realm`. It should work there the way you want. https://realm.io/ – shaktiman_droid Apr 11 '16 at 14:00
  • @Jabbar_Jigariyo The issue with Realm is that it looks to be a pain to use with Retrofit... I would have to write a custom deserializer and some people using them both seems to have thread issue. – Jaythaking Apr 11 '16 at 14:30
  • Actually it isn't that painful to use Realm with retrofit. Also, they are improving so fast that whatever problem you might have faced before few months has probably been fixed already. I'm successfully using Realm + Retrofit in a production app. – shaktiman_droid Apr 11 '16 at 14:34
  • @Jabbar_Jigariyo Did you find some good link of example using them both? I do want to try it, but I have hard times finding examples... I'm also using Jackson for databinding, would that need to be changed too you think? – Jaythaking Apr 11 '16 at 14:40
  • I think you read through both this entire page: https://realm.io/docs/java/latest/ and all the sample application in the Realm github project. That would definitely give you a good head-start. – shaktiman_droid Apr 11 '16 at 14:42

1 Answers1

0

Check out ONAM (Object Nested Access Management), an MIT licensed library - https://github.com/basilgregory/ONAM4Android.

It’s a simple ORM / persistence framework for sqlite in Android. Lightweight too. Supports relationships including nested persistence, finder methods, transactions and much more.

(Voluntary Disclosure: I am part of the team that maintains ONAM)