title: How to represent nested one to many relationship in android room?
body: <p>Here are <strong>sample POJO</strong>:</p>
  <pre><code>@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;
  }
  </code></pre>
  <p>Here is my <strong>UsersWithPets</strong> class to fetch all the users along with pets.</p>
  <pre><code>public class UserWithPets {
    @Embedded
    public User user;
    @Relation(parentColumn = &quot;id&quot;, entityColumn = &quot;userId&quot;, entity =   Pet.class)
    public List&lt;Pet&gt; pets;
  }
  </code></pre>
  <p><strong>My user Dao</strong></p>
  <pre><code>@Dao
  public interface UserDao {
    @Query(&quot;SELECT * FROM User&quot;)
    List&lt;UserWithPets&gt; loadUsersWithPets();
  }
  </code></pre>
  <p><strong>and userdb</strong></p>
  <pre><code>@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,
                            &quot;User.db&quot;)
                            .build();
                }
            }
        }
        return INSTANCE;
    }
  }
  </code></pre>
  <p>The &quot;<strong>UserWithPets</strong>&quot; embeds user object and relates with its associated list of &quot;<strong>Pet</strong>&quot;, but how do I fetch list of &quot;<strong>Category</strong>&quot; if my &quot;<strong>Pet</strong>&quot; POJO can have 1-many relationship with &quot;<strong>Category</strong>&quot;.</p>
  <p>Also the user DAO only returns all the &quot;<strong>User</strong>&quot; along with its &quot;List&lt;<strong>Pet</strong>&gt;&quot; by mapping the id between <code>user.id</code> and <code>pet.userId</code>, if my &quot;<strong>Pet</strong>&quot; POJO can have many &quot;<strong>Category</strong>&quot;(List of Category), how do I create my DAO and abstract database class such that querying for a particular &quot;<strong>User</strong>&quot; id will return me the &quot;<strong>User</strong>&quot; object along with its &quot;<strong>List of Pets</strong>&quot; and individual &quot;<strong>Pet</strong>&quot; containing a &quot;List of <strong>Category&quot;</strong> and querying for all will return all the &quot;<strong>List of Users</strong>&quot; wherein each User contains a &quot;<strong>List of Pet</strong>&quot; and each <strong>Pet</strong> contains a &quot;<strong>List of Category</strong>&quot;</p>