My current goal is to have a one-to-many relation in my data.
From various tutorials I've come to understand that I have two main ways of doing this.
Create two entities and have the child use the @ForeignKey
annotation
Create these two entities and an extra POJO, which uses @Embedded
for the parent entity and @Relation
to have a list of children tied to the parent
So, using Relation just seems like extra effort. Or am I missing something?
Is the big advantage of Relation this:
When the Pojo is returned from a query, all of its relations are also fetched by Room.
Which would not be as easy with the first method?
From Gautam's comment and further reading I now understand it this way:@Relation
is a convenience option to make retrieving connected entities easier. The price for this convenience is to give up the ability to control what happens on parent entry deletion and possibly other things.
when you use @relation, it help the DBMS (database management system) to create a tree for sorting and storing data. so when you want to run any query it is very helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With