从Room Persistence映射对象

时间:2018-03-19 19:53:20

标签: android-room

我有课 - Subject.java

    @Entity(tableName = Constants.SUBJECT_ENTITY)
        public class Subject {

        @PrimaryKey
        private int id;

        @ColumnInfo(name = "name")
        private String name;
        ...
    }

Grade.java

    @Entity(tableName = Constants.GRADE_ENTITY,
            foreignKeys = @ForeignKey(
                    entity = Subject.class,
                    onDelete = CASCADE,
                    parentColumns = "id",
                    childColumns = "subjectId"))
    public class Grade implements Serializable {

        @PrimaryKey
        private int id;

        @ColumnInfo(name = "semester")
        private int semester;

        @ColumnInfo(name = "subjectId")
        private int subjectId;
        ...
    }

SubjectAndAllGrades.java

    public class SubjectAndAllGrades {

        public int id;
        public String name;

        @Relation(parentColumn = "id", entityColumn = "subjectId")
        public List<Grade> oceny;
        ...
    }

我在SubjectDAO中执行了一些查询

    @Query("SELECT distinct s.id as id, s.name as name " +
            "FROM subject s, grade g " +
            "WHERE s.id = g.subjectId " +
            "AND g.semester = :semester ")
    Flowable<List<SubjectAndAllGrades>> getSubjectAndAllItsGradesForSemester(int semester);

在这个查询中你可能会猜到我试图选择所有具有特定学期成绩的科目。但是,此查询的结果返回通过subjectId键连接的SubjectAndAllGrades对象的列表,但不考虑条件     g.semester =:学期 我怎么能得到这个查询?

0 个答案:

没有答案