0Pricing
Android Academy · Lesson

Entities and DAOs

Define tables and data access objects.

Entities and DAOs is a free Android Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Room?

Room is a persistence library that provides an abstraction layer over SQLite. It lets you work with database rows as Kotlin objects with compile-time checked queries.

The Three Room Building Blocks

  • Entity — a table, represented as a data class.
  • DAO — Data Access Object, defines queries.
  • Database — ties entities and DAOs together (next lesson).

Defining an Entity

Annotate a data class with @Entity. Each property becomes a column.

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "users")
data class User(
    @PrimaryKey val id: Int,
    val name: String,
    val email: String
)

Primary Keys

Every entity needs a @PrimaryKey. To let Room generate IDs automatically, use autoGenerate.

@Entity
data class Note(
    @PrimaryKey(autoGenerate = true)
    val id: Int = 0,
    val text: String
)

Customizing Columns

Use @ColumnInfo to rename a column or set its type affinity.

import androidx.room.ColumnInfo

@Entity
data class User(
    @PrimaryKey val id: Int,
    @ColumnInfo(name = "full_name")
    val name: String
)

What Is a DAO?

A DAO is an interface annotated with @Dao. It declares the methods your app uses to read and write data. Room generates the implementation for you.

Insert, Update, Delete

Room provides convenience annotations for the basic operations.

@Dao
interface UserDao {
    @Insert
    suspend fun insert(user: User)

    @Update
    suspend fun update(user: User)

    @Delete
    suspend fun delete(user: User)
}

Conflict Strategies

When inserting a row that already exists, choose a strategy with onConflict.

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun upsert(user: User)

Custom Queries with @Query

The @Query annotation lets you write SQL. Room checks it at compile time against your schema.

@Query("SELECT * FROM users WHERE id = :id")
suspend fun getById(id: Int): User?

Why suspend Functions?

Database I/O must not run on the main thread. Marking DAO methods suspend lets Room run them off the main thread when called from a coroutine, preventing UI jank.

Compile-Time Safety

A major Room benefit: if you mistype a column name in a @Query, the build fails. You catch errors early instead of at runtime.

Quick Check

Test your understanding of entities and DAOs.

Recap

You learned:

  • @Entity defines a table as a data class with a @PrimaryKey.
  • @Dao declares query/insert/update/delete methods.
  • @Query SQL is checked at compile time.
  • DAO methods should be suspend for off-main-thread I/O.

Frequently asked questions

Is the “Entities and DAOs” lesson free?

Yes — the full text of “Entities and DAOs” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “Entities and DAOs”?

Define tables and data access objects. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Entities and DAOs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Entities and DAOs
  2. The Room Database Class
  3. Queries and Relationships
  4. Observing Data with Flow
← Back to Android Academy