Junior — Middle
Can you explain what the Embedded annotation means in the Room library?
sobes.tech AI
Answer from AI
The @Embedded annotation in the Room library is used to include the fields of one object into another database entity as part of its columns. This allows avoiding the creation of a separate table for the nested object and storing its fields directly in the parent entity's table.
For example, if you have a class Address with fields street and city, and you want to include it in the User entity, then using @Embedded, the street and city fields will be added as columns in the User table.
data class Address(
val street: String,
val city: String
)
@Entity
data class User(
@PrimaryKey val id: Int,
val name: String,
@Embedded val address: Address
)
As a result, the User table will contain columns: id, name, street, city. This is convenient for data normalization and simplifying queries.