如何在房间数据库中添加前缀?

how to add prefix in roomDatabase?

我的 roomDatabase 有问题。

我在 google 上搜索并找到了一些解决方案,但无法解决我的问题。

谁能帮帮我?

注意:我想在我的项目中使用Object Oriented

错误:

多个字段具有相同的列名:class_id。字段名称:classId, classId.

类条目:

@Entity(tableName = "class")

public class ClassEntry {

@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = COLUMN_CLASS_ID)
private int classId;

@ColumnInfo(name = "name")
private String className;
@ColumnInfo(name = "day")
private String classDay;

public ClassEntry(int classId, String className, String classDay) {
    this.classId = classId;
    this.className = className;
    this.classDay = classDay;
}

@Ignore
public ClassEntry(int classId) {
    this.classId = classId;
}

学生条目:

@Entity(tableName = "student",
    primaryKeys = {COLUMN_CLASS_ID, COLUMN_STUDENT_ID},
    foreignKeys = {@ForeignKey(
            entity = ClassEntry.class,
            parentColumns = COLUMN_CLASS_ID,
            childColumns = COLUMN_CLASS_ID,
            onDelete = CASCADE)})
public class StudentEntry extends ClassEntry{

@ColumnInfo(name = COLUMN_CLASS_ID)
private int classId;
@NonNull
@ColumnInfo(name = COLUMN_STUDENT_ID)
private String studentId;
@ColumnInfo(name = "first_name")
private String firstName;
@ColumnInfo(name = "last_name")
private String lastName;

public StudentEntry(int classId, @NotNull String studentId, String firstName, String lastName) {
    super(classId);
    this.studentId = studentId;
    this.firstName = firstName;
    this.lastName = lastName;
}

您的问题是您正在使用 ClassEntry [=64] 扩展 StudentEntry class =].这不是您使用 Room 管理相关数据的方式。你实际上是在说你想要一个 table 里面。而不是你想要一个(class)对多(学生)的关系。

因此,您需要一个包含 ClassEntry 和相应 StudentEntries(0 或更多)的对象。为此,您创建了一个普通的 POJO class 嵌入父项 table 并关联子项 class(es).

所以您的 ClassEntry class 没问题。但是,您的 StudentEntry class 可能是:-

@Entity(tableName = "student",
        primaryKeys = {COLUMN_CLASS_ID, COLUMN_STUDENT_ID},
        foreignKeys = {@ForeignKey(
                entity = ClassEntry.class,
                parentColumns = COLUMN_CLASS_ID,
                childColumns = COLUMN_CLASS_ID,
                onDelete = CASCADE)})
public class StudentEntry /* extends ClassEntry <<<<<<<<<< COMMENTED OUT */ {

    @ColumnInfo(name = COLUMN_CLASS_ID)
    private int classId;
    @NonNull
    @ColumnInfo(name = COLUMN_STUDENT_ID)
    private String studentId;
    @ColumnInfo(name = "first_name")
    private String firstName;
    @ColumnInfo(name = "last_name")
    private String lastName;

    public StudentEntry(int classId, @NonNull String studentId, String firstName, String lastName) {
        /* super(classId); <<<<<<<<<< COMMENTED OUT */
        this.classId = classId; //<<<<<<<<<< ADDED
        this.studentId = studentId;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @NonNull
    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(@NonNull String studentId) {
        this.studentId = studentId;
    }

    public int getClassId() {
        return classId;
    }

    public void setClassId(int classId) {
        this.classId = classId;
    }
}
  • 即它不会扩展 ClassEntry,因此不会调用 ClassEntry super。看评论。
  • 注意删除了多余的 getter 和 setter

然后你添加另一个 POJO class 例如:-

class ClassEntryWithStudentEntrys {

    @Embedded
    ClassEntry classEntry;
    @Relation(entity = StudentEntry.class,parentColumn = COLUMN_CLASS_ID,entityColumn = COLUMN_CLASS_ID)
    List<StudentEntry> studentEntryList;
}
  • @Embedded 包括以下 class
  • @Relation 定义关系

道的可能是:-

@Dao
interface AllDao {

    @Insert
    long insert(ClassEntry classEntry);
    @Insert
    long insert(StudentEntry studentEntry);
    @Transaction
    @Query("SELECT * FROM class")
    List<ClassEntryWithStudentEntrys> getAllClassEntriesWithStudentEntries();

}
  • 最后一个使用带有嵌入式和关系注释的 POJO。

使用上面的工作示例

考虑以下代码,这添加了 2 Classes Class1 和 Class2。 Class1 已添加 2 个学生,Class2 已添加 3 个学生。 最后使用 getAllClassEntriesWithStudentEntries 查询提取 ClassEntriesWithStudnetEntries:-

    dao = db.getAllDao();

    long class1 = dao.insert(new ClassEntry(0,"Class1","Mon"));
    dao.insert(new StudentEntry((int)class1,"student1","Mary","Smith"));
    dao.insert(new StudentEntry((int) class1,"student2","Fred","Bloggs"));
    long class2 = dao.insert(new ClassEntry(0,"Class2","Tue"));
    dao.insert(new StudentEntry((int) class2,"student3","Sarah","Tomms"));
    dao.insert(new StudentEntry((int)class2,"student4","Alan","Richardson"));
    dao.insert(new StudentEntry((int)class2,"student5","Jayne","Lockyer"));

    List<ClassEntryWithStudentEntrys> classWithStudentsList = dao.getAllClassEntriesWithStudentEntries();
    for (ClassEntryWithStudentEntrys c: classWithStudentsList) {
        Log.d("CLASSSTUDENTINFO","Class is " + c.classEntry.getClassName() + " on Day " + c.classEntry.getClassDay() + " ID is " + c.classEntry.getClassId());
        for (StudentEntry s: c.studentEntryList) {
            Log.d("CLASSSTUDENTINFO","\tStudent is " + s.getFirstName() + "," + s.getLastName() + " Student ID is " + s.getStudentId());
        }
    }

结果 作为日志输出

D/CLASSSTUDENTINFO: Class is Class1 on Day Mon ID is 1
D/CLASSSTUDENTINFO:     Student is Mary,Smith Student ID is student1
D/CLASSSTUDENTINFO:     Student is Fred,Bloggs Student ID is student2
D/CLASSSTUDENTINFO: Class is Class2 on Day Tue ID is 2
D/CLASSSTUDENTINFO:     Student is Sarah,Tomms Student ID is student3
D/CLASSSTUDENTINFO:     Student is Alan,Richardson Student ID is student4
D/CLASSSTUDENTINFO:     Student is Jayne,Lockyer Student ID is student5
  • 注意以上只是一个演示,不打算重新运行。