• 10

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Entity inheritance in android room

I have a parent class Queue which has following fields:

  1. Name
  2. Type
  3. Status

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;

public class ProductionQueue extends Queue {
  String startDate;
  String endDate

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?


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.

  • 1
Reply Report
    • Does this mean that each subclass will have its own data as well as a mirrored record in parent table? I wanted to avoid sub tables because mostly I would only be working with the parent class data fields.
      • 2
    • No, it doesn't. You will only have the rows you save in the tables in which you save them; there won't be any mirrored records unless you create them. If you are only going to be working with a few fields at a time, then you can use the child entity and replace SELECT * with SELECT id, name, type, status whenever necessary.
      • 1
    • So I do not have to make Queue class an entity and can just use it as an abstract class? and for querying all queue subclasses together, I should create an extra Dao class which can query all subtables and concatenate their results or is there a better way?
    • To be honest, I don't know. It depends on what exactly you are trying to achieve, what your real data looks like, what kind of queries you are going to be doing, et cetera. Your question was about inheritance, and my answer is that, whilst it can be done, you were doing it wrong. In any case, as I said, if all you want to do is to retrieve only a few fields from a table, you don't need inheritance at all, but rater a projection operation (i.e., a SELECT statement with specific fields).