Android 安卓房间@多对多关系?
我正在开发一个android应用程序,并在android操作系统中使用新的架构组件:LiveData、ViewModel和Room。 我在Room实现中遇到了一个小问题,即创建一个@Relation,它返回连接查询的结果(多对多关系) 我的数据库结构如下所示:Android 安卓房间@多对多关系?,android,many-to-many,android-room,android-livedata,Android,Many To Many,Android Room,Android Livedata,我正在开发一个android应用程序,并在android操作系统中使用新的架构组件:LiveData、ViewModel和Room。 我在Room实现中遇到了一个小问题,即创建一个@Relation,它返回连接查询的结果(多对多关系) 我的数据库结构如下所示: @Entity public class Student{ @PrimaryKey private int id; private String name; private String email; } @Entit
@Entity
public class Student{
@PrimaryKey
private int id;
private String name;
private String email;
}
@Entity
public class Group{
@PrimaryKey
private int id;
private String name;
}
@Entity(foreignKeys = {
@ForeignKey(entity = Student.class,
parentColumns = "id",
childColumns = "student_id"),
@ForeignKey(entity = Group.class,
parentColumns = "id",
childColumns = "group_id")
})
public class StudentGroup{
private int studentId;
private int groupId;
}
我怎么能只为一个特定的学生建立所有的小组,像这样
public class StudentWithGroups{
@Relation(parentColumn = "id", entityColumn = "rule_id", entity =
StudentGroup.class)
private List<Group> groups;
}
公共班级学生分组{
@关系(parentColumn=“id”,entityColumn=“rule\u id”,entity=
学生组(班级)
私人名单组;
}
我已经检查了像和这样的问题
我怎样才能让所有的小组只针对特定的学生,诸如此类
在这方面,我有:
@Query(“从类别中选择类别。*\n”+
“内部联接客户\类别\在类别上联接。id=客户\类别\联接。类别id\n”+
“其中customer\u category\u join.customerId=:customerId”)
列出客户的分类(字符串customerId);
将其转换为实体会产生如下结果:
@Query("SELECT Group.* FROM Group\n"+
"INNER JOIN StudentGroup ON Group.id=StudentGroup.groupId\n"+
"WHERE StudentGroup.studentId=:studentId")
List<Group> groupsForStudent(String studentId);
@Query(“从组中选择组。*\n”+
“组上的内部联接StudentGroup.id=StudentGroup.groupId\n”+
“其中StudentGroup.studentId=:studentId”)
列出学生组(字符串studentId);
一般来说,使用Room,计算出SQL忽略Room,然后在DAO中使用该SQL。通过对In-Room的介绍,您可以轻松处理多对多关系
将学生表和组表的主键修改为:
@Entity
public class Student{
@PrimaryKey
private int sId;
private String name;
private String email;
}
@Entity
public class Group{
@PrimaryKey
private int gId;
private String name;
}
@Entity(foreignKeys = {
@ForeignKey(entity = Student.class,
parentColumns = "sId",
childColumns = "studentId"),
@ForeignKey(entity = Group.class,
parentColumns = "gId",
childColumns = "groupId")
})
public class StudentGroup{
private int studentId;
private int groupId;
}
您可以通过以下方式获取特定学生的所有组:
public class StudentWithGroups{
@Embedded
Student student;
@Relation(
parentColumn = "sId",
entity = Group.class,
entityColumn = "gId",
associateBy = @Junction(
value = StudentGroup.class,
parentColumn = "studentId",
entityColumn = "groupId)
)
List<Group> groups;
}
公共班级学生分组{
@嵌入
学生;
@关系(
parentColumn=“sId”,
实体=组.class,
entityColumn=“gId”,
associateBy=@Junction(
value=StudentGroup.class,
parentColumn=“studentId”,
entityColumn=“groupId)
)
列出小组;
}
现在,您可以通过以下方式查询数据库中的结果:
@Dao
public interface StudentDao {
@Query("SELECT * FROM Student")
List<StudentWithGroups> getGroupsOfStudent();
}
@Dao
公共接口StudentDao{
@查询(“从学生中选择*)
列出getGroupsOfStudent();
}
是的,我可以通过使用某种联接的查询来完成,但我想知道是否有办法将此查询的结果放入保留关系的类中。我的意思是,希望从某个表及其所有关系数据中获取整个对象。例如,如果我有表Student、Groups和StudentGroup,我希望StudentntDao将返回Studets表中的所有数据以及组对象列表。希望能解释清楚:)@MrVasilev:我认为这在今天的房间里是不可能的。谢谢。我的研究以相同的答案结束。希望在下一个版本中添加。你已经为房间创建了功能请求了吗?我认为这将是一个非常好的功能e、 即使内部实现在一个事务中使用两个不同的查询,如果Room能够检测到@Relation字段中何时可以使用“join”实体和完整的M:N,您也可以节省大量代码和时间您已经为文件室创建了功能请求了吗?--抱歉,没有。
@Dao
public interface StudentDao {
@Query("SELECT * FROM Student")
List<StudentWithGroups> getGroupsOfStudent();
}