java – Saving child collections with OrmLite on Android with objects created from Jackson-ThrowExceptions

Exception or error:

I have a REST service which I’m calling from my app, which pulls in a JSON object as a byte[] that is then turned into a nice nested collection of objects — all of that bit works fine. What I then want to do is persist these objects to SQLite storage using OrmLite, and that’s where things start to break down, because as I understand, OrmLite doesn’t automatically persist nested objects.

For simplicity, let’s strip out my actual functionality and let my objects be modelled simply as followed:

@DatabaseTable(tableName = "parents")
public class Parent {

    @DatabaseField(id=true)
    private String name;

    @ForeignCollectionField
    // have to use Collection here because needs to be compatible with Jackson
    private Collection<Child> children; 

    /* Getters and setters go here */
}

@DatabaseTable(tableName = "children")
public class Child {

    @DatabaseField(id=true)
    private String name;

    @DatabaseField(foreign=true)
    private Parent parent;

    /* Getters and setters go here */
}

What happens is that when adding creating a new Parent object in the database with my corresponding parentDao object, the children are not being persisted along with the parent.

This is a common question and has been raised before, there are definitely other questions on SO which are very similar to this one, most notably Saving nested foreign objects with ORMLite on Android in answer to which Gray suggests creating the child object before the parent which would work fine for creating my POJOs manually.

However, I’ve not yet seen an answer which factors in cases where the objects are being generated by another library (in this case Jackson). Without digging deeper into Jackson’s deserialisation innards (which removes the whole benefit of its simplicity) and trying to get Jackson to create the child objects and then add them to the parent (unless I’m missing something and this is easier than it sounds?) that doesn’t seem to be a particularly attractive solution in this particular case.

Equally, there are other solutions which involve adding further annotations to the foreign key which seem to be suitable for working with single child objects, but those annotations aren’t available when working with @ForeignCollectionFields and their corresponding Collections.

How to solve:

You might want to try using a ForeignCollection instead of a Collection in your parent class.

From the ORMLite-Documentation:

“The foreign collections support the add() and remove() methods in which case the objects will be both added or removed from the internal list if the collection is eager, and DAO calls will be made to affect the [child] table as well for both eager and lazy collections.”

(http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_2.html#Foreign-Collection)

On the documentation of the class itself it also states that the add / remove is forwarded to the database. (http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/dao/ForeignCollection.html#add(T))

I’ve never worked with this, though, so I just hope the documentation is correct and this helps you solving your problem. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *