可以在会议室数据库的构造方法中使用@Ignore字段吗?

时间:2019-08-20 10:55:14

标签: java android android-room pojo android-database

我有一个room database。在我的Entity(PerformanceEntry)中,我不想在表中保存一个字段(字符串studentName),但是我想访问列表中的字段。

我使用@Ignore进行字段设置,但是在那种情况下,我也不能在构造函数中使用字段。

我想知道有什么解决办法吗?还是我必须在表中创建字段?

注意: 该字段是一个临时值,我不想将其保存到表中。

@Entity(tableName = "performance")
public class PerformanceEntry {

@ColumnInfo(name = "entry_id")
    @PrimaryKey(autoGenerate = true)
    private int entryId;
    @ColumnInfo(name = "class_id")
    private int classId;
    @ColumnInfo(name = "student_id")
    private String studentId;
    @ColumnInfo(name = "class_date")
    private String classDate;
    private boolean absent;
    private boolean delay;
    private int positive;
    private int negative;

    @Ignore
    private String studentName;

    public PerformanceEntry(int entryId, int classId, String studentId, String classDate, boolean absent, boolean delay, int positive, int negative, String studentName) {
    this.entryId = entryId;
    this.classId = classId;
    this.studentId = studentId;
    this.classDate = classDate;
    this.absent = absent;
    this.delay = delay;
    this.positive = positive;
    this.negative = negative;
    this.studentName = studentName;
    }
}

错误:

error: Cannot find setter for field.

2 个答案:

答案 0 :(得分:1)

您可以使用注释@Transient代替@Ignore

答案 1 :(得分:0)

尝试将@PrimaryKey字段放在@Ignore字段下方。