Let's say I have an Ingredient entity, and a Recipe entity. Lots of recipes might refer to the same ingredient.
recipe 1 uses ingredients 1, 2 and 3
recipe 2 uses ingredients 1, 3 and 5
I want to be able to load the ingredients from the recipe, but not the reverse.
I think I need to model this as a Many-To-Many relation by creating a separate entity to track all the recipe <-> ingredient mappings.
Is my understanding correct, or is there some way to make this work with a ToMany relation?
I don't think using a single ToMany relation will work, because GreenDao requires them to have a foreign key in the target entity back to the original entity (see "Modelling To-Many Relations" in the docs). This means that ingredients can only refer to a single recipe - as soon as two recipes reference the same ingredient then one of them will lose its relation.
FTR, if you don't set up the foreign key in the target entity, then the ToMany relation is lost after restarting the application (i.e. it only works for the current database session). Tip: to simulate this scenario in your automated tests, just call daoSession.clear()
before loading the object and asserting that it contains the values you expect.