Hayden
Hayden

Reputation: 2988

Android Room Allow Dao @Query to populate @Ignore columns

I'm wanting my Dao to populate @Ignore columns in my Entity class. For example:

Entity

@Entity(tableName = "example")
data class Example(
    @PrimaryKey
    val id: Long,
    val value: Int
) {
    @Ignore
    var nextId: Long = 0L
}

Dao

@Dao
interface ExampleDao {
    @Query(value = "SELECT *, (id + 1) AS nextId FROM example")
    fun getAllExamples(): List<Example>
}

However, when the application gets built, the following warning gets produced: The query returns some columns [nextId] which are not used by com.example.app.Example and it doesn't populate nextId.

Is it possible to include @Ignore columns in a @Query (if so, how)? If not, what are some strategies that can be employed to populate columns that are not present in my tables into my Entity class.

Note: I'm fully aware with the example provided that I can simply do something like:

@Ignore
val nextId: Long = id + 1

But is not the point of the question I am asking.

Upvotes: 6

Views: 984

Answers (1)

Hayden
Hayden

Reputation: 2988

Based on the information that @CommonsWare has given me, the solution that I went with is

data class ExampleWithNextId(
    @Embedded
    val example: Example) {
    var nextId: Long = 0L
}

Then use it in Dao like so

@Dao
interface ExampleDao {
    @Query(value = "SELECT *, (id + 1) AS nextId FROM example")
    fun getAllExamplesWithNextId(): List<ExampleWithNextId>
}

Upvotes: 1

Related Questions