我是Android Room持久性库的新手。我正在尝试设计学校和学生班。
一所学校可以列出一组学生。有我要支持的查询。
学校课程:
@Entity
public class School {
@PrimaryKey
@ColumnInfo(name = "school_id")
public String schoolId;
@ColumnInfo(name = "school_name")
public String schoolName;
}
学生班:
@Entity(foreignKeys = @ForeignKey(entity = School.class,
parentColumns = "school_id",
childColumns = "school_id",
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE))
public class Student {
@PrimaryKey
@ColumnInfo(name = "student_id")
public String studentId;
@ColumnInfo(name = "student_name")
public String studentName;
@ColumnInfo(name = "school_id")
public String schoolId;
}
我正在尝试了解应该如何编写我的Dao层。这是我的尝试。
道老师:
@Dao
public interface StudentDao {
@Query(SELECT * FROM Student WHERE school_id = :schoolId)
List<Student> getSudents(String schoolId);
@Query("DELETE FROM Student WHERE student_id = :studentId")
void deleteStudent(String studentId);
@Query("UPDATE Student SET student_name = :newName WHERE student_id = :studentId")
void updateStudent(String studentId, String newName);
}
这种使用Student Dao的方式是否正确,还是应该使用@Relations来支持1:N关系?