微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在roomDatabase中添加前缀?

如何解决如何在roomDatabase中添加前缀?

我的 roomDatabase 有问题。

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

有人可以帮我吗?

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

错误

多个字段具有相同的 columnName: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 类扩展 StudentEntry 类。这不是您使用 Room 管理相关数据的方式。你实际上是在说你想要一张桌子。而是因为您想要一种(班级)对多(学生)的关系。

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

所以您的 ClassEntry 类很好。但是您的 StudentEntry 类可能是 :-

@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,因此不会调用 Class Entry super。查看评论。
  • 注意删除了多余的 getter 和 setter

然后添加另一个 POJO 类,例如:-

class ClassEntryWithStudentEntrys {

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

道的可能是:-

@Dao
interface AllDao {

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

}
  • 最后一个使用带有 Embedded 和 Relation 注释的 POJO。

使用上述内容的工作示例

考虑下面的代码,这增加了 2 个类 Class1 和 Class2。 Class1增加了2名学生,Class 2增加了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
  • 请注意,以上只是一个演示,不打算重新运行。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。