与数据库的单个列配合使用的查询错误

我正在将Android建筑组件与Room库一起使用。基本上,我想从两个不同查询中的两个单独的列中获取结果。如果有人可以帮助或建议我如何系统化我的实体,我将感到非常高兴。

@Entity(tableName = "note_table")
public class Note {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id_col")
    private int id;

    @Ignore
    @ColumnInfo(name = "checks_col")       
    private ArrayList<Checks> checks = new ArrayList<>();

    @ColumnInfo(name = "res_col")
    private String result;

    public Note(String result,ArrayList<Checks> checks) {
        this.checks = checks;
        this.result = result;
    }

    public void setId(int id) {this.id = id;}
    public int getId() {return id;}
    public void setResult(String result){this.result = result;}
    public String getResult() {return result;}
    public void setChecks(ArrayList<Checks> checks){this.checks = checks;}
    @TypeConverters({Converters.class})
    public ArrayList<Checks> getchecks() {return checks;}

}
@Dao
public interface NoteDao {

    @Insert
    void insert(Note note);

    @Update
    void update(Note note);

    @Delete
    void delete(Note note);

    @Query("DELETE FROM note_table")
    void deleteAllNotes();

    @Query("SELECT * FROM note_table")
    LiveData<List<Note>> getallNotes();

    @Query("SELECT res_col FROM note_table ORDER BY res_col DESC LIMIT 1 ") // <-- this is my try //
    LiveData<Note> getResultNote();

}

错误在下面给出:

The columns returned by the query does not have the fields [id] . Even though they are annotated as non-null or primitive. Columns returned by the query: [res_col]

警告信息如下:

Note has some fields [id_col] which are not returned by the query. If they are not supposed to be read from the result,you can mark them with @Ignore annotation. You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: res_col. Fields in Note: id_col,res_col.
idarkness 回答:与数据库的单个列配合使用的查询错误

LiveData<Note> getResultNote();

希望返回 Note 对象,因为某些列被注释或隐含为非空(即{strong> int 为private int id;是Java原语)那么它不能为null),那么就不能为要由查询返回的 String 构建注释

您希望getResultNote()返回字符串,而不是注释

或者使用SELECT * .....代替SELECT res_col ......返回Note,然后从 Note 获取字符串。

本文链接:https://www.f2er.com/3167444.html

大家都在问