Android Academy · Lesson

Entities and DAOs

Define tables and data access objects.

Lesson 1 of 413 steps

Entities and DAOs is a free Android Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 4 lessons in total.

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.
Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
36
Lessons
152

Frequently Asked Questions

Is the “Entities and DAOs” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “Entities and DAOs”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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