简体   繁体   English

如何在android机房中表示嵌套的一对多关系?

[英]How to represent nested one to many relationship in android room?

Here are sample POJO: 以下是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 to fetch all the users along with pets. 这是我的UsersWithPets类,用于获取所有用户以及宠物。

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

My user DAO 我的用户DAO

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

and userdb 和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;
  }
}

The "UserWithPets" embeds user object and relates with its associated list of "Pet", but how do I fetch list of "Category" if my "Pet" POJO can have 1-many relationship with "Category". “UserWithPets”嵌入用户对象并与其关联的“Pet”列表相关联,但如果我的“Pet”POJO与“Category”具有1-many关系,如何获取“Category”列表。 Also the user DAO only returns all the "User" along with its "List" by mapping the id between user.id and pet.userId, if my "Pet" POJO can have many "Category"(List of Category), how do I create my DAO and abstract database class such that querying for a particular "User" id will return me the "User" object along with its "List of Pets" and individual "Pet" containing a "List of Category" and querying for all will return all the "List of Users" wherein each User contains a "List of Pet" and each Pet contains a "List of Category" 用户DAO也只通过映射user.id和pet.userId之间的id返回所有“User”及其“List”,如果我的“Pet”POJO可以有很多“Category”(Category of Category),怎么做我创建了我的DAO和抽象数据库类,以便查询特定的“用户”ID将返回“User”对象及其“Pets of Pets”和包含“Category of List”的个人“Pet”并查询所有将返回所有“用户列表”,其中每个用户包含“宠物列表”,每个宠物包含“类别列表”

You need to map your query to a POJO/Java Bean class like PetWithCategories: 您需要将查询映射到像PetWithCategories这样的POJO / Java Bean类:

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

Probably it would be easier to start additional database requests after you finished the first one. 完成第一个数据库请求后,可能更容易启动其他数据库请求。 I don't see any way to realize that via @Relation or with inner joins as this is not a single value you want to return. 我没有看到任何方法通过@Relation或内部联接来实现,因为这不是您想要返回的单个值。 You could use left join, but that would 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM