1

大家好,我在这里需要真正的帮助,我的整个应用程序都依赖于此。我正在尝试在 android 房间数据库中的 2 个实体之间创建一对多的关系。我想知道如何提取两个实体的数据,我可以显示狗的名字和所有者的名字,以及如何为此实现 recyclerview 适配器。以下代码是我从开发人员文档中得出的. 提前致谢

    @Entity(tableName = "table_dog")
public class Dogs {
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "dog_id")
    private int dogId;

    @ColumnInfo(name = "dog_name")
    private String dogName;

    @ColumnInfo(name = "dog_owner_id")
    private int dogOwnerId;

    public Dogs(String dogName, int dogOwnerId) {
        this.dogName = dogName;
        this.dogOwnerId = dogOwnerId;
    }

    public int getDogId() {
        return dogId;
    }

    public void setDogId(int dogId) {
        this.dogId = dogId;
    }

    public String getDogName() {
        return dogName;
    }

    public void setDogName(String dogName) {
        this.dogName = dogName;
    }

    public int getDogOwnerId() {
        return dogOwnerId;
    }

    public void setDogOwnerId(int dogOwnerId) {
        this.dogOwnerId = dogOwnerId;
    }
}
@Entity(tableName = "table_dog")
public class Dogs {
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "dog_id")
    private int dogId;

    @ColumnInfo(name = "dog_name")
    private String dogName;

    @ColumnInfo(name = "dog_owner_id")
    private int dogOwnerId;

    public Dogs(String dogName, int dogOwnerId) {
        this.dogName = dogName;
        this.dogOwnerId = dogOwnerId;
    }

    public int getDogId() {
        return dogId;
    }

    public void setDogId(int dogId) {
        this.dogId = dogId;
    }

    public String getDogName() {
        return dogName;
    }

    public void setDogName(String dogName) {
        this.dogName = dogName;
    }

    public int getDogOwnerId() {
        return dogOwnerId;
    }

    public void setDogOwnerId(int dogOwnerId) {
        this.dogOwnerId = dogOwnerId;
    }
}
public class DogOwner {
    @Embedded
    public Owner owner;

    @Relation(
            parentColumn = "owner_id",
            entityColumn = "dog_owner_id",
            entity = Dogs.class
    )
    LiveData<List<Dogs>> dogs;
}
@Dao
public interface DogOwnerDao {

    @Query("select * from table_owner")
    LiveData<List<DogOwner>> getAllDogs();

    @Insert
    void insert(DogOwner dogOwner);
}
4

1 回答 1

0

我认为您正在寻找类似于INNER JOIN的行为。

通过使用以下类来获取数据,他们会收到一份所有者列表,然后每个所有者都有一个狗列表。

public class DogOwner {
    @Embedded
    public Owner owner;

    @Relation(
            parentColumn = "owner_id",
            entityColumn = "dog_owner_id",
            entity = Dogs.class
    )
    LiveData<List<Dogs>> dogs; //Shouldn't it just be list of dogs?
}

也就是说,您将必须填充两个 RecyclerView,一个用于显示所有者列表,另一个用于实现特定所有者的列表。

要在单个列表中获取所有者和狗数据,您需要实现类似于INNER JOIN的情况:

如果您运行以下 SQL 语句,您可以看到每条狗都包含它的主人信息。

SELECT * FROM table_dog INNER JOIN table_owner ON table_dog.dog_owner_id = table_owner.owner_id

将其应用于您的课程将是:


public class DogOwner {
    @Embedded
    public Dogs dog;
    
    @Relation(
            entityColumn = "owner_id",
            parentColumn = "dog_owner_id",
            entity = Owner.class
    )
    public Owner owner;
}

道:

@Dao
public interface DogOwnerDao {
    
    @Transaction
    @Query("SELECT * FROM table_dog")
    LiveData<List<DogOwner>> getAllDogs();
}

通过这种方式,您将已经拥有每只狗的主人数据,并且您只需要实现一个 RecyclerView。

Kotlin 示例:

@Entity
data class Owner constructor(
    var name: String,
    var age: Int,
    var address: String,
    @PrimaryKey(autoGenerate = true) var id: Long = 0,
)  
@Entity(
    foreignKeys = arrayOf(
        ForeignKey(
            entity = Owner::class,
            parentColumns = arrayOf("id"),
            childColumns = arrayOf("ownerId"),
            onDelete = ForeignKey.CASCADE,
            onUpdate = ForeignKey.CASCADE
        )
    )
)
data class Dog constructor(
    var name: String,
    var age: Int,
    val ownerId: Long,
    @PrimaryKey(autoGenerate = true) var id: Long = 0
)
data class DogWithOwner(
    @Embedded val dog: Dog,
    @Relation(
        entityColumn = "id",
        parentColumn = "ownerId",
        entity = Owner::class
    )
    val owner: Owner
)
@Dao
interface DogWithOwnerDao {
    @Transaction()
    @Query("SELECT * FROM Dog")
    suspend fun getAll(): List<DogWithOwner>

    @Transaction()
    @Query("SELECT * FROM Dog WHERE id=:id")
    suspend fun getById(id: Int): DogWithOwner
}
于 2021-05-30T09:58:01.057 回答