javaormliteforeign-collection

ORMLite initialize foreign collection


I have foreign collection field on my Object. If I create Object > save it > query for it : then I have it and can use it. But if I create Object and try to access foreign collection field right away then it's null. How can I initialize it?


Solution

  • As part of the ORMLite Dao class, there is an assignEmptyForeignCollection(...) method. This creates an empty collection for you and assigns it to the proper field.

    To quote from the Javadocs for the method:

    Creates an empty collection and assigns it to the appropriate field in the parent object. This allows you to add things to the collection from the start. For example let's say you have an Account which has the field: