I have two entities with a foreign key relation: product
and 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;
}
}
I want to select all fields for both entities. I've defined a separate entity for it, with @Embedded
annotation:
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;
}
}
Now I can create a query like this:
@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);
The problem is that I have to manually specify all fields which becomes too verbose if I have entities with 5 - 10 fields. Is it possible to use some wildcard approach without manually specifying all fields?