@Entity(tableName = "post_entity") data class PostEntity( var path :String, var content:String, var time:Long, var likeCount:Int, @PrimaryKey(autoGenerate = true) var id: Int = 0 ) @Dao interface PostDao { @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun insertPost(post: PostEntity) @Query("SELECT * FROM post_entity") suspend fun getAll(): List<PostEntity> } 编译报错 The query returns some columns [path, content, time, likeCount, id] which are not used by java.lang.Object. You can use @ColumnInfo annotation on the fields to specify the mapping. You can annotate the method with @RewriteQueriesToDropUnusedColumns to direct Room to rewrite your query to avoid fetching unused columns. You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: path, content, time, likeCount, id. Fields in java.lang.Object: . public abstract java.lang.Object getAll(@org.jetbrains.annotations.NotNull ^ 帮我改正
时间: 2023-06-23 16:09:54 浏览: 160
这个错误是由于你的查询返回的字段与实体类中定义的字段不一致所导致的。为了解决这个错误,你需要在实体类中使用 @ColumnInfo 注解来显式指定每个字段的映射关系,指定与查询返回的字段相同的名称。另外,你可以在 DAO 接口的方法上添加 @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH) 注解来抑制警告。此外,还可以使用 @RewriteQueriesToDropUnusedColumns 注解来让 Room 重写查询以避免获取未使用的列。以下是修改后的代码:
@Entity(tableName = "post_entity")
data class PostEntity(
@ColumnInfo(name = "path") var path: String,
@ColumnInfo(name = "content") var content: String,
@ColumnInfo(name = "time") var time: Long,
@ColumnInfo(name = "likeCount") var likeCount: Int,
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") var id: Int = 0
)
@Dao
interface PostDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertPost(post: PostEntity)
@SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)
@Query("SELECT * FROM post_entity")
suspend fun getAll(): List<PostEntity>
}
阅读全文