javaandroidkotlinandroid-room

Room: @Embedded vs @TypeConverters


If we have @Entity in Room in which there are complex objects as fields, we can use 2 approaches:

@Embedded - as I understand it, if you mark the field with this annotation, then Room will save all the fields that are inside the object as fields of the Entity itself, and then retrieve everything correctly.

@TypeConverters - here we write our own converter, which in most cases boils down to the usual parsing of an object into a Json string.

Actually the question:

What is the fundamental difference? Why not write @Embedded everywhere and not worry about converters? When is it better to write your own converter, and when is it better to use @Embedded, and what are the advantages and disadvantages of these approaches?


Solution

  • If we have @Entity in Room in which there are complex objects as fields, we can use 2 approaches

    Actually there is one more approach to be mentioned here - is saving complex object in separate table and linking to it with foreign key (as usual id with Int or Long type, often autogenerated).