android - GreenDao is it possible to do a To-Many relation in only one direction? -
let's have ingredient entity, , recipe entity. lots of recipes might refer same ingredient.
recipe 1 uses ingredients 1, 2 , 3 recipe 2 uses ingredients 1, 3 , 5
i want able load ingredients recipe, not reverse.
i think need model many-to-many relation creating separate entity track recipe <-> ingredient mappings.
is understanding correct, or there way make work tomany relation?
i don't think using single tomany relation work, because greendao requires them have foreign key in target entity original entity (see "modelling to-many relations" in docs). means ingredients can refer single recipe - 2 recipes reference same ingredient 1 of them lose relation.
ftr, if don't set foreign key in target entity, tomany relation lost after restarting application (i.e. works current database session). tip: simulate scenario in automated tests, call daosession.clear()
before loading object , asserting contains values expect.
since 1 ingredient can part of many recipes , 1 recipe consists of many ingredients relation many-to-many relation.
the common way model in db store relation in separate table.
this independent of use of greendao. achieve model using greendao have create new entity recipetoingredient
, build relations using entity.
update
if free in designing database can use schema this:
table ingredienttype -------------------------- id | integer (pk) description | text table recipe -------------------------- id | integer (pk) description | text table ingredient -------------------------- id_recipe | integer (fk) id_type | integer (fk) unit | text quantity | integer
this means tomany-relation ingredient recipe , igredient ingredienttype. technically many2many-mapping additional attributes between ingredienttype , recipe.
in greendao use 2 tomany-relations model this.
Comments
Post a Comment