簡體   English   中英

獲取數據加入房間中具有相同列 id 的兩個表

[英]Get data joining two table in room having same column id

嗨,我正在使用兩個表 student 和 student_address 在兩個表中都有共同的列 id 作為 uid(在任何情況下都不能更改)

我使用 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);

我得到了第一張桌子的價值,但沒有得到第二張桌子的價值。 在數據庫中,兩個表都存在

我認為,您的問題的根源是您給@Embedded 的前綴:

@Embedded(prefix = "type_")
private StudentAddress studentAddress;

由於該房間無法匹配查詢結果中的字段名稱和 class StudentAndAddress 中的字段。 您必須擺脫前綴(然后 filelds 的名稱將相等)或更改地址表查詢中的別名(將前綴添加到它們) - 如下所示:

@Query("SELECT student.*,addresses.[field1] as type_[field1], adsresses.address_uid as type_address_uid, ... FROM student  INNER JOIN student_address as addresses ..."

在房間里,我們需要使用關系來解決這種類型的查詢

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM