Skip to content
tezvyn:

How do you define a Room table for a Kotlin data class?

Source: developer.android.comEasyHow cards are made

How do you define a Room table for a Kotlin data class?

Tests your grasp of the minimum Room entity contract. A strong answer cites Entity and PrimaryKey, notes Kotlin defaults work out of the box, and mentions ColumnInfo for renaming.

What's really being asked

This question checks whether you understand the minimum annotation contract Room requires to map a Kotlin data class to a SQLite table. At the senior level, interviewers expect you to know that Room is annotation-driven, that Kotlin data classes are first-class citizens, and that only a small subset of properties actually need explicit annotations. The question also reveals whether you confuse the entity layer with DAO or Database abstractions.

The full answer

Four things in order. First, the class itself must be annotated with Entity, which tells Room to create a corresponding table; you can optionally supply a table name if you want it to differ from the class name. Second, at least one property must be annotated with PrimaryKey, because Room enforces a primary key on every table; you can set autoGenerate to true if it is an integer type. Third, Room automatically maps Kotlin properties to columns using the property names and types, so no additional annotations are required for basic fields. Fourth, ColumnInfo is optional and only used when you need the database column name to differ from the Kotlin property name.

The mistakes people make

Three red flags stand out. One, claiming that every property needs an explicit ColumnInfo or other annotation; Room defaults are designed to reduce boilerplate. Two, omitting PrimaryKey entirely or suggesting a table can exist without one; Room will throw a compile-time error if it is missing. Three, confusing the Entity annotation with Database or Dao, such as describing query methods or repository logic instead of schema definition.

What usually comes next

Expect the interviewer to ask how you handle embedded objects with Embedded, how you define indices or foreign keys inside the Entity annotation, or how Room handles Kotlin non-null types versus nullable Java types. They may also probe migrations, asking what happens when you add a new property to this User class and ship an update.

A concrete example

A minimal User entity looks like this in concept. A Kotlin data class named User is annotated with Entity. Inside, an integer property id is marked with PrimaryKey and autoGenerate set to true. String properties like name and email require no extra annotations because Room infers them automatically. If you wanted the email column stored as user_email, you would place ColumnInfo with name set to user_email on that property. That is the entire essential surface area.

Interview question

What is the minimum annotation set for a Kotlin data class to serve as a Room entity?

  • a.@Entity on the class, @PrimaryKey on one property, and @ColumnInfo on all remaining properties
  • b.@Entity on the class and @ColumnInfo on every property
  • c.@Entity on the class and @PrimaryKey on at least one propertyCorrect
  • d.@Entity on the class with no other annotations required
Why?

Room automatically infers column names and types from Kotlin properties, so only @Entity and @PrimaryKey are required. The most tempting distractor adds @ColumnInfo everywhere, which is unnecessary boilerplate since Room handles basic mapping by default.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles