Android 房间库 - select 嵌入实体的所有字段
Android Room library - select all fields for embedded entities
我有两个具有外键关系的实体:product
和 category
。
@Entity(primaryKeys = "id")
public class Product {
public final long id;
@NonNull
public final String name;
@ForeignKey(entity = Category.class, parentColumns = "id", childColumns = "categoryId")
public final long categoryId;
public Product(long id, @NonNull String name, long categoryId) {
this.id = id;
this.name = name;
this.categoryId = categoryId;
}
}
@Entity(primaryKeys = "id")
public class Category {
public final long id;
@NonNull
public final String name;
public Category(long id, @NonNull String name) {
this.id = id;
this.name = name;
}
}
我想 select 两个实体的所有字段。我已经为它定义了一个单独的实体,带有 @Embedded
注释:
public class ProductWithCategory {
@NonNull
@Embedded(prefix = "product_")
public final Product product;
@NonNull
@Embedded(prefix = "category_")
public final Category category;
public ProductWithCategory(@NonNull Product product, @NonNull Category category) {
this.product = product;
this.category = category;
}
}
现在我可以创建这样的查询:
@Query("SELECT product.id as product_id, product.name as product_name, product.categoryId as product_categoryId, category.id as category_id, category.name as category_name FROM product JOIN category on categoryId = category.id WHERE product.id = :id")
LiveData<ProductWithCategory> getProduct(long id);
问题是我必须手动指定所有字段,如果我的实体有 5 - 10 个字段,这会变得太冗长。是否可以在不手动指定所有字段的情况下使用一些通配符方法?
最后我用@Relation
注解解决了这个问题。唯一的缺点是我必须为它使用 List
或 Set
,即使在这种情况下它是 0 或 1 个实体:
public class ProductWithCategory {
@NonNull
@Embedded
public final Product product;
@Relation(parentColumn = "categoryId", entityColumn = "id", entity = Category.class)
public List<Category> category;
public ProductWithCategory(@NonNull Product product) {
this.product = product;
}
}
但这简化了查询:
@Query("SELECT * FROM product WHERE product.id = :id")
LiveData<ProductWithCategory> getProduct(long id);
我有两个具有外键关系的实体:product
和 category
。
@Entity(primaryKeys = "id")
public class Product {
public final long id;
@NonNull
public final String name;
@ForeignKey(entity = Category.class, parentColumns = "id", childColumns = "categoryId")
public final long categoryId;
public Product(long id, @NonNull String name, long categoryId) {
this.id = id;
this.name = name;
this.categoryId = categoryId;
}
}
@Entity(primaryKeys = "id")
public class Category {
public final long id;
@NonNull
public final String name;
public Category(long id, @NonNull String name) {
this.id = id;
this.name = name;
}
}
我想 select 两个实体的所有字段。我已经为它定义了一个单独的实体,带有 @Embedded
注释:
public class ProductWithCategory {
@NonNull
@Embedded(prefix = "product_")
public final Product product;
@NonNull
@Embedded(prefix = "category_")
public final Category category;
public ProductWithCategory(@NonNull Product product, @NonNull Category category) {
this.product = product;
this.category = category;
}
}
现在我可以创建这样的查询:
@Query("SELECT product.id as product_id, product.name as product_name, product.categoryId as product_categoryId, category.id as category_id, category.name as category_name FROM product JOIN category on categoryId = category.id WHERE product.id = :id")
LiveData<ProductWithCategory> getProduct(long id);
问题是我必须手动指定所有字段,如果我的实体有 5 - 10 个字段,这会变得太冗长。是否可以在不手动指定所有字段的情况下使用一些通配符方法?
最后我用@Relation
注解解决了这个问题。唯一的缺点是我必须为它使用 List
或 Set
,即使在这种情况下它是 0 或 1 个实体:
public class ProductWithCategory {
@NonNull
@Embedded
public final Product product;
@Relation(parentColumn = "categoryId", entityColumn = "id", entity = Category.class)
public List<Category> category;
public ProductWithCategory(@NonNull Product product) {
this.product = product;
}
}
但这简化了查询:
@Query("SELECT * FROM product WHERE product.id = :id")
LiveData<ProductWithCategory> getProduct(long id);