2

After looking through the android documentation and many blog posts/youtube videos, it's still unclear to me when one would favor using @Embedded with @Relation versus using a @ForeignKey relationship.

Are the @Embedded/@Relation annotations intended to be used only with relational objects that map 1 entity to other?

5
  • > "Are the @Embedded/@Relation annotations intended to be used only with relational objects that map 1 entity to other?" No. In my understanding, the @Embedded annotation does not refer to relation in SQL sense. Instead, it refers to nesting. Basically, if you add an embedded POJO to your entity, every field in the embedded class become actual columns in your entity and you can query the fields in the embedded class as part of the columns in this entity. You can see that in the official docs example Commented Mar 3, 2020 at 15:16
  • Thanks @SegunFamisa So embedded objects don't have to be entities themselves? This seems like it's just for convenience right? Like if a POJO contains another object that isn't used anywhere else in the schema, you can just embed it instead of making another table for that object and adding a foreign key relationship? Commented Mar 3, 2020 at 16:18
  • That's right, embedded objects don't have to be entities themselves. You can use it together with @Relation for more complex relations as shown in the @Relation doc Commented Mar 3, 2020 at 16:37
  • Hmm interesting. So I guess foreign keys are more useful for when all the objects are stored in separate tables or when you need cascading behavior. Commented Mar 3, 2020 at 19:44
  • 1
    Yes. That's precisely what a foreign key is used for. Commented Mar 4, 2020 at 8:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.