Android Room 允许 Dao @Query 填充 @Ignore 列

Hay*_*den 6 android kotlin android-room

我希望我的 Dao 填充我的 Entity 类中的 @Ignore 列。例如:

实体

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

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

但是,当构建应用程序时,会生成以下警告: The query returns some columns [nextId] which are not used by com.example.app.Example并且它不会填充nextId.

是否可以在 @Query 中包含 @Ignore 列(如果可以,如何)?如果没有,可以采用哪些策略将表中不存在的列填充到实体类中。

注意:我完全了解这个示例,只要我可以简单地执行以下操作:

@Ignore
val nextId: Long = id + 1
Run Code Online (Sandbox Code Playgroud)

但这不是我要问的问题的重点。

Hay*_*den 1

根据@CommonsWare给我的信息,我采用的解决方案是

data class ExampleWithNextId(
    @Embedded
    val example: Example) {
    var nextId: Long = 0L
}
Run Code Online (Sandbox Code Playgroud)

然后像这样在 Dao 中使用它

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