Junior — Middle
Can you name the key components of the Room library for database operations?
sobes.tech AI
Answer from AI
Key components of the Room library:
- Entity — a class that describes a table in the database. Each field corresponds to a column.
- DAO (Data Access Object) — an interface with methods for accessing data (insertion, update, deletion, queries).
- Database — an abstract class inheriting from RoomDatabase, which links Entity and DAO, and provides a database instance.
Example:
@Entity
data class User(
@PrimaryKey val id: Int,
val name: String
)
@Dao
interface UserDao {
@Query("SELECT * FROM User")
fun getAll(): List<User>
@Insert
fun insert(user: User)
}
@Database(entities = [User::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}