1

Hello guys i need real help over here my whole app depends on that. I'm trying to create one to many relationship between 2 entities in android room database. I want to know how how can i extract the data of both entities that i can display the name of the dog and the name of the owner and how to implement the recyclerview adapter for that.The following code is what i came up from developer docs. Thanks in advance

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

1 Answers1

0

I think you are looking for behavior similar to INNER JOIN.

By using the following class to obtain the data, they are receiving a list of owners and in turn each owner has a list of dogs.

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?
}

That is, you will have to fill two RecyclerViews, one to display the list of owners and the other to implement the list of a specific owner.

To get the owner and dog data in a single list, you need to implement a situation similar to INNER JOIN:

If you run the following SQL statement, you can see that each dog contains its owner information.

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

Applying this to your class it would be:


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

Dao:

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

In this way you will already have the owner's data for each dog and you only have to implement a RecyclerView.

Example with 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
}