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