I have a parent class Queue which has following fields:
Based on the type, I have decomposed Queue into further subclasses, each of which has its own extra fields. For example:
ProductionQueue extends Queue { startDate, endDate }
ShippingQueue extends Queue { shippingDate, shippingAgent }
...
I have created Queue as a base class in android studio and extending my subclasses from it but the code fails to compile and complains about missing fields (subclass fields). Here is an example:
@Entity(tableName = "queue")
public class Queue {
int id;
String name;
int type;
int status;
}
@Entity
public class ProductionQueue extends Queue {
String startDate;
String endDate
}
@Dao
public interface ProductionQueueDao extends BaseDao<ProductionQueue> {
@Query("SELECT * FROM queue WHERE id = :queueID")
LiveData<List<ProductionQueue>> findByID(Long queueID);
}
On compilation I receive: error: The columns returned by the query does not have the fields [startDate, endDate] in com.example.room.ProductionQueue even though they are annotated as non-null or primitive. Columns returned by the query: [id,name,type,status].
[id,name,type,status]
are coming from parent class while [startDate, endDate]
are subclass fields.
On various occasions in my application, I would only list the queue names, type, and status and I would prefer to keep queue data in a single table for quick querying.
I can create sub tables for individual subclasses and use relations but if Room allows inheritance all of this can be easily avoided. Does room support such inheritance if yes how can I make it work?
Thanks.
The problem is that you are trying to retrieve production queues from the table queue
, which doesn't contain the information about dates that production queues need.
To solve it, define a name for the production queues table:
@Entity(tableName = "production_queue")
public class ProductionQueue extends Queue {
...
}
Then, change your query to retrieve rows from the correct table:
// Retrieving rows from "production_queue", not from "queue"
@Query("SELECT * FROM production_queue WHERE id = :queueID")
LiveData<List<ProductionQueue>> findByID(Long queueID);
This way, the rows returned will include the fields startDate
and endDate
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With