I've recently started using LiveData in my project and was facing difficulties JOINing data from related tables. Not being able to find anything related to this on the web - links welcome, please, I started this morning to try and find a solution.
So, I put together a Product Entity class…
@Entity(tableName = "products")
public class Product {
@PrimaryKey(autoGenerate = true)
@NonNull
int productID;
@NonNull
private String productName;
@NonNull
private String supplierName;
private int supplierID;
private int stocklevel;
public Product(@NonNull String productName, int supplierID) {
this.productName = productName;
this.supplierID = supplierID;
this.supplierName = "Dummy Supplier";
stocklevel = 0;
}
and a DAO with the query…
@Query("SELECT products.*, suppliers.supplierName FROM products JOIN suppliers ON products.supplierID = suppliers.supplierID ORDER BY stocklevel DESC")
LiveData<List<Product>> getProductDetails();
so that, when the query is run, the placeholder text is overwritten by the supplierName from the related table. And it works!
So, my question is, 'Is this an acceptable way of proceeding? And, if not - what is?'
Any feedback? Or links?