How to represent nested relationships to each other in an Android room?

Here's an example POJO:

@Entity
public class User {
  @PrimaryKey
  public String id;
}

@Entity
public class Pet {
  @PrimaryKey
  public String id;
  public String userId;
  public String petName;
}

@Entity
public class Category {
  @PrimaryKey
  public String id;
  public String petId;
  public String categoryName;
}

      

Here is my UsersWithPets class for fetching all users along with their pets.

public class UserWithPets {
  @Embedded
  public User user;
  @Relation(parentColumn = "id", entityColumn = "userId", entity =   Pet.class)
  public List<Pet> pets;
}

      

My DAO user

@Dao
public interface UserDao {
  @Query("SELECT * FROM User")
  List<UserWithPets> loadUsersWithPets();
}

      

and userdb

@Database(entities = {User.class, Pet.class, Category.class}, version = 1)
@TypeConverters(DateConverter.class)
public abstract class UsersDb extends RoomDatabase {

  private static UsersDb INSTANCE;

  public abstract UserDao userDao();

  public static UsersDb getInstance(Context context) {
      if (INSTANCE == null) {
          synchronized (UsersDb.class) {
              if (INSTANCE == null) {
                  INSTANCE = Room.databaseBuilder(context.getApplicationContext(), UsersDb.class,
                          "User.db")
                          .build();
              }
          }
      }
      return INSTANCE;
  }
}

      

UserWithPets embeds a custom object and binds to its associated Pet list, but how do I get the Category list if my Pet POJO can have 1-many relationships with Categories. Also, the DAO user returns the total "User" along with his "list", matching the id between user.id and pet.userId, if my POJO "Pet" can have many "Category" (Category List) how to do it I create my DAO class and abstract databases, so that a request for a specific user id will return me a User object along with its own “list of pets” and a separate “pet” containing a “List of categories” and a query for all will return the whole ”User List "where each User contains a" Pet List "and each pet contains a" Category List "

+3


source to share


2 answers


You need to map your request to a POJO / Java Bean class like PetWithCategories:



public class UserWithPets {
  @Embedded
  public User user;
  @Relation(parentColumn = "id", entityColumn = "userId", entity = Pet.class)
  public List<PetWithCategories> pets;
}

public class PetWithCategories {
  @Embedded
  public Pet pet;
  @Relation(parentColumn = "id", entityColumn = "petId")
  public List<Category> categories;
}

      

+3


source


It would probably be easier to run additional database queries after you finish the first one. I don't see any way to figure this out via @Relation or with inner joins, as this is not one value you want to return. You can use a left join, but this will give you multiple pets, only with different category objects.



List<UserWithPets> list = userDao.loadUsersWithPets();
for(UserWithPets user : list) {
   for(Pet pet : user.pets) {
      pet.categories = categorieDao.getCategoriesByPetId(pet.id); 
      //pet.categories should have the @Ignore annotation
   }
}

...

@Dao
public interface CategoryDao {

   @Query("SELECT * FROM Category WHERE petId = :petId")
   List<Categories> getCategoriesByPetId(int petId);
}

      

0


source







All Articles