我有一个Room Query,它返回POJO
的几个字段。例如。查询仅返回group_id
,group_name
,但是POJO
具有更多字段,例如
sender_name
,消息等。如果我编译代码,它将给我一个错误。我看到自己的构建失败,但是没有与ROOM
相关的错误。
我想知道POJO
中是否有比查询返回内容更多的字段。因为如果我仅将group_Id
和group_name
保留在POJO
中,则编译成功。
@Query("SELECT group_id,group_name FROM group")
fun getGroups(): DataSource.Factory<Int, Chat>
class Chat {
@ColumnInfo(name = "group_id")
var groupId: String = ""
@ColumnInfo(name = "group_name")
var groupName: String? = null
var sender: String? = null
@ColumnInfo(name = "created_by")
var createdBy: String? = null
@ColumnInfo(name = "message_id")
var messageId: String? = null
@ColumnInfo(name = "message")
var text: String? = null
}
答案 0 :(得分:0)
您需要将POJO类注释为(Optional)
,
@Entity(tableName = "group")
class Chat {
@ColumnInfo(name = "group_id")
var groupId: String = ""
@ColumnInfo(name = "group_name")
var groupName: String? = null
var sender: String? = null
@ColumnInfo(name = "created_by")
var createdBy: String? = null
@ColumnInfo(name = "message_id")
var messageId: String? = null
@ColumnInfo(name = "message")
var text: String? = null
}
,然后将您的退货类型更改为
@Query("SELECT group_id,group_name FROM group")
fun getGroups(): List<Chat>
(这可能会消除您的编译错误)。