Search code examples
android-studiokotlinandroid-roomdao

Proper Way to annotate Android Room Dao?


I followed codelab tutorials and ended up with confusion. I want to use external db for testing purpose, the only one table and column(word_table and word as mentioned in codelab) works fine. But i'm trying to add my own created .db with following sql info:

CREATE TABLE "product_table" (
    "id"    INTEGER NOT NULL,
    "product"   TEXT,
    "partof"    TEXT,
    PRIMARY KEY("id")
);

I am not sure how to correctly annotate it. Following are the codes i'm using.

WordDao.kt

@Dao
interface WordDao {

@Query("SELECT * from product_table ORDER BY id ASC")
fun getAlphabetizedId(): List<Id>

@Query("SELECT * from product_table ORDER BY word ASC")
fun getAlphabetizedWords(): LiveData<List<Word>>

@Query("SELECT * from product_table ORDER BY partof ASC")
fun getAlphabetizedPartof(): List<Partof>

Product.kt

@Entity(tableName = "eilian_table")
class Id(@PrimaryKey @ColumnInfo(name = "id") val id: String)

@Entity(tableName = "eilian_table")
class Word(@ColumnInfo(name = "word") val word: String)

@Entity(tableName = "eilian_table")
class Partof(@ColumnInfo(name = "partof") val partof: String)

Getting symbol and reference error.


Solution

  • Have a look through the docs - basically an Entity represents a table, you're creating three for the same table, and each one has one field. Here's your SQL:

    CREATE TABLE "product_table" (
        "id"    INTEGER NOT NULL,
        "product"   TEXT,
        "partof"    TEXT,
        PRIMARY KEY("id")
    );
    

    So you want something like this:

    @Entity(tablename = "product_table")
    data class Product(
        @PrimaryKey
        val id: Int,
        val product: String,
        @ColumnInfo(name = "partof")
        val partOf: String
    )
    

    and you can see how it basically maps to the SQL - you might want to make the Strings nullable, and I threw in the @ColumnInfo annotation just as an example of where the Kotlin field name is different from the column name in the table (even though it's case-insensitive so it doesn't actually matter here).

    You'll need to change your queries so you're only pulling the columns you want out of the table