android - Android Room 允许 Dao @Query 填充 @Ignore 列
问题描述
我希望我的 Dao 在我的 Entity 类中填充 @Ignore 列。例如:
实体
@Entity(tableName = "example")
data class Example(
@PrimaryKey
val id: Long,
val value: Int
) {
@Ignore
var nextId: Long = 0L
}
道
@Dao
interface ExampleDao {
@Query(value = "SELECT *, (id + 1) AS nextId FROM example")
fun getAllExamples(): List<Example>
}
但是,在构建应用程序时,会产生以下警告:
The query returns some columns [nextId] which are not used by com.example.app.Example
并且它不会填充nextId
.
是否可以在 @Query 中包含 @Ignore 列(如果是,如何)?如果没有,可以采用哪些策略将表中不存在的列填充到我的实体类中。
注意:我完全了解示例,前提是我可以简单地执行以下操作:
@Ignore
val nextId: Long = id + 1
但这不是我要问的问题的重点。
解决方案
根据@CommonsWare 给我的信息,我采用的解决方案是
data class ExampleWithNextId(
@Embedded
val example: Example) {
var nextId: Long = 0L
}
然后像这样在 Dao 中使用它
@Dao
interface ExampleDao {
@Query(value = "SELECT *, (id + 1) AS nextId FROM example")
fun getAllExamplesWithNextId(): List<ExampleWithNextId>
}
推荐阅读
- python - aws codebuild 是否仍支持 python2.7 运行时?
- rust - 如何序列化为切片数组?
- c++ - 我们可以使用 std::atomic 使 S[x++] = str atomic 吗?
- aws-sdk-net - 在哪里可以找到适用于 .NET 的 AWSSDK.S3 中的错误代码枚举
- dictionary - 谷歌地图不显示公司标记
- javascript - 使用 split 时生成两个变量
- mongodb - 如何在 MongoDB 的内部数组文档中执行 AND 查询?
- pandas - Pandas 总结(暨和计数)列
- python - 使用其他列的字符串过滤器创建新的数据框列
- python - Tensorflow 对象检测 API:“FailedPreconditionError:HashTable 对同一键具有不同的值。键项 { 具有 0 并尝试添加值 4”