CPS251 Android Development by Scott Shaper

Creating Entities and DAOs

Entities and DAOs are the foundation of your Room Database. Think of an Entity as the blueprint for what you want to store, and a DAO as the set of instructions for how to work with that data. Together, they make sure your app knows exactly what information to keep and how to find it later.

Entities: Defining Your Data

An Entity is a data class that tells Room what kind of information you want to save. Each Entity becomes a table in your database, and each property becomes a column in that table.

Example: Note Entity (from RoomDatabaseDemo)


// Defines the 'Note' data class, which represents a table in the Room database.
// The tableName is explicitly set to "notes".
@Entity(tableName = "notes")
data class Note(
    // Primary key for the 'notes' table, automatically generated by the database.
    @PrimaryKey(autoGenerate = true) val id: Int = 0,
    // The title of the note.
    val title: String,
    // The main content/body of the note.
    val content: String,
    // The date when the note was created or last updated.
    val date: String
) 

The Note data class is an essential part of our Room database, as it defines the structure of the data we want to store. Think of it as the blueprint for a single entry (a row) in our 'notes' table. Here's a breakdown of what each part signifies:

In summary, the Note entity provides a clear, structured way to represent and store note data within your Room database. Each instance of a Note object corresponds to a row in the 'notes' table, with its properties mapping directly to the table's columns.

DAOs: Working With Your Data

A DAO (Data Access Object) is an interface that lists all the ways you can interact with your data—like adding, finding, or deleting notes.

Example: NoteDao (from RoomDatabaseDemo)


// Note Data Access Object (DAO).
// This interface defines the methods for interacting with the 'notes' table in the database.
@Dao
interface NoteDao {
    // Query to retrieve all notes from the 'notes' table, ordered by date in descending order.
    // Returns a Flow, which emits updates whenever the data in the table changes.
    @Query("SELECT * FROM notes ORDER BY date DESC")
    fun getAllNotes(): Flow<List<Note>>

    // Inserts a new note or replaces an existing one if there's a conflict (e.g., same primary key).
    // 'suspend' keyword indicates that this is a coroutine function and can be paused and resumed.
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(note: Note)

    // Deletes an existing note from the database.
    // 'suspend' keyword indicates that this is a coroutine function.
    @Delete
    suspend fun delete(note: Note)
} 
        

This DAO lets you get all notes, add a new note, or delete a note.

Understanding the `NoteDao` Interface

The NoteDao (Data Access Object) is an interface that defines how your application interacts with the data stored in the `notes` table of your Room database. It acts as a bridge between your application's logic and the database operations, abstracting away the raw SQL queries.

1. DAO Annotation and Interface Definition


@Dao
interface NoteDao {
    // ...
}

2. Querying All Notes


    @Query("SELECT * FROM notes ORDER BY date DESC")
    fun getAllNotes(): Flow<List<Note>>

3. Inserting a Note


    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(note: Note)

4. Deleting a Note


    @Delete
    suspend fun delete(note: Note)

Tips for Success

Common Mistakes to Avoid

Best Practices