获取在具有相同列 ID 的房间中连接两个 table 的数据
Get data joining two table in room having same column id
您好,我正在使用两个 table 学生和 student_address,在这两个 table(在任何情况下都无法更改)
我正在使用 class StudentAndAddress 作为
public class StudentAndAddress{
@Embedded
private Student student;
@Embedded(prefix = "type_")
private StudentAddress studentAddress;
public Address getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public StudentAddress getStudentAddress() {
return studentAddress;
}
public void setStudentAddress(StudentAddress studentAddress) {
this.studentAddress = studentAddress;
}
}
我正在使用的查询:
@Query("SELECT * FROM student INNER JOIN student_address ON student.uid == student_address.address_uid and address_link.linkto_type==:linkToType")
LiveData<List<StudentAndAddress>> getStudentAndAddress(String linkToType);
我正在获取第一个 table 的值,但没有获取第二个 table 的值。
在数据库中 table 都在那里
我认为,问题的根源是您给@Embedded 的前缀:
@Embedded(prefix = "type_")
private StudentAddress studentAddress;
那个房间无法匹配查询结果中的字段名称和 class StudentAndAddress 中的字段。您必须摆脱前缀(然后 filelds 的名称将相等)或更改地址查询中的别名 table(将您的前缀添加到它们中)- 像这样:
@Query("SELECT student.*,addresses.[field1] as type_[field1], adsresses.address_uid as type_address_uid, ... FROM student INNER JOIN student_address as addresses ..."
在 room 中,我们需要使用关系解决此类查询
public class StudentAndAddress{
@Embedded
private StudentAddress studentAddress;
@Relation(
parentColumn = "address_uid",
entityColumn = "uid",
entity = Student.class
)
private Student student;
public Address getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public StudentAddress getStudentAddress() {
return studentAddress;
}
public void setStudentAddress(StudentAddress studentAddress) {
this.studentAddress = studentAddress;
}
}
我们需要写的查询是:-
@Query("SELECT * FROM student_address WHERE linkto_type==:linkToType")
LiveData<List<StudentAndAddress>> getStudentAndAddress(String linkToType);
您好,我正在使用两个 table 学生和 student_address,在这两个 table(在任何情况下都无法更改)
我正在使用 class StudentAndAddress 作为
public class StudentAndAddress{
@Embedded
private Student student;
@Embedded(prefix = "type_")
private StudentAddress studentAddress;
public Address getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public StudentAddress getStudentAddress() {
return studentAddress;
}
public void setStudentAddress(StudentAddress studentAddress) {
this.studentAddress = studentAddress;
}
}
我正在使用的查询:
@Query("SELECT * FROM student INNER JOIN student_address ON student.uid == student_address.address_uid and address_link.linkto_type==:linkToType")
LiveData<List<StudentAndAddress>> getStudentAndAddress(String linkToType);
我正在获取第一个 table 的值,但没有获取第二个 table 的值。 在数据库中 table 都在那里
我认为,问题的根源是您给@Embedded 的前缀:
@Embedded(prefix = "type_")
private StudentAddress studentAddress;
那个房间无法匹配查询结果中的字段名称和 class StudentAndAddress 中的字段。您必须摆脱前缀(然后 filelds 的名称将相等)或更改地址查询中的别名 table(将您的前缀添加到它们中)- 像这样:
@Query("SELECT student.*,addresses.[field1] as type_[field1], adsresses.address_uid as type_address_uid, ... FROM student INNER JOIN student_address as addresses ..."
在 room 中,我们需要使用关系解决此类查询
public class StudentAndAddress{
@Embedded
private StudentAddress studentAddress;
@Relation(
parentColumn = "address_uid",
entityColumn = "uid",
entity = Student.class
)
private Student student;
public Address getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public StudentAddress getStudentAddress() {
return studentAddress;
}
public void setStudentAddress(StudentAddress studentAddress) {
this.studentAddress = studentAddress;
}
}
我们需要写的查询是:-
@Query("SELECT * FROM student_address WHERE linkto_type==:linkToType")
LiveData<List<StudentAndAddress>> getStudentAndAddress(String linkToType);