Android Room-处理对象中的对象列表和查询结果 [英] Android Room - Handling List of Objects in an Object and querying result
问题描述
我正在使用Android的Room Database,并且在了解如何操作时遇到了一些大问题:
Im working with Android's Room Database and am having some big problems understanding how to:
- 当我在数据库中添加
Person
时,它会添加其所有变量,例如List<Shoe>
,List<Pet>
等也添加到数据库中. -
创建关系,这样当我检索
Person
时,将检索其所有字段,例如Pet, Shoe, Shirt
等.(不确定是哪种类型的查询)
- When I add a
Person
to the database, it adds all its variables e.g.List<Shoe>
,List<Pet>
etc. to the database as well. Create relations such that when I retrieve a
Person
, all its fields are retrieved e.gPet, Shoe, Shirt
etc. (Not sure what type of query that is)
执行简单查询,例如Retrieve Person where
shoe.name = "boot"
;
Perform simple query e.g. Retrieve Person where
shoe.name= "boot"
;
我知道您必须对对象列表使用外键关系,否则对于单个对象可以使用@Embed
或@TypeConverter
I know you must use Foreign Key relationships with list of objects, otherwise with a single object one can use @Embed
or @TypeConverter
示例代码如下所示;
@Entity(tableName = "person")
public class Person {
@PrimaryKey
@NonNull
private String personId;
private List<Pet> pets;
private List<Shoe> shoes;
private List<Shirt> shirts;
}
@Entity(foreignKeys = {
@ForeignKey(
entity = Person.class,
parentColumns = "personId",
childColumns = "personIdFk"
)
})
public class Pet {
String petId;
String name;
String personIdFk; //Foreign key to person
}
@Entity(foreignKeys = {
@ForeignKey(
entity = Person.class,
parentColumns = "personId",
childColumns = "personIdFk"
)
})
public class Shoe {
String shoeId;
String name;
String personIdFk; //Foreign key to person
}
@Entity(foreignKeys = {
@ForeignKey(
entity = Person.class,
parentColumns = "personId",
childColumns = "personIdFk"
)
})
public class Shirt {
String shirtId;
String name;
String personIdFk; //Foreign key to person
}
推荐答案
Room doesn't support this directly due to some potential issues with lazy loading, but with some DAO trickery it is possible. You'll need to handle the insertions explicitly, and to query everything at once you'll need a POJO to wrap it all.
@Entity(foreignKeys = {
@ForeignKey(
entity = PersonEntity.class,
parentColumns = "personId",
childColumns = "personIdFk",
onDelete = CASCADE
)
})
public class Pet {
@PrimaryKey
private String petId;
private String name;
private String personIdFk;
}
@Entity(tableName = "person")
public class PersonEntity {
@PrimaryKey
private String personId;
}
public class Person {
@Embedded
private PersonEntity personEntity;
@Relation(parentColumn = "personId", entityColumn = "personIdFk")
private List<Pet> pets;
}
@Dao
public abstract class PersonDao {
@Insert
protected abstract void insert(PersonEntity personEntity);
@Insert
protected abstract void insert(List<Pet> pets);
@Transaction
public void insert(Person person) {
insert(person.getEntity());
insert(person.getPets());
}
@Query("SELECT * FROM person")
public abstract List<Person> getAll();
}
@Database(entities = {PersonEntity.class, Pet.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract PersonDao personDao();
}
为简洁起见,省略了构造函数,getter和setter.
Constructors, getters and setters omitted for brevity.
这篇关于Android Room-处理对象中的对象列表和查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!