【发布时间】:2019-05-07 16:32:19
【问题描述】:
我有一个父类 Queue,它有以下字段:
- 姓名
- 类型
- 状态
根据类型,我将 Queue 分解为更多的子类,每个子类都有自己的额外字段。例如:
ProductionQueue extends Queue { startDate, endDate }
ShippingQueue extends Queue { shippingDate, shippingAgent }
...
我在 android studio 中创建了 Queue 作为基类并从中扩展了我的子类,但代码无法编译并抱怨缺少字段(子类字段)。这是一个例子:
@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);
}
编译时我收到:错误:查询返回的列在 com.example.room.ProductionQueue 中没有字段 [startDate, endDate],即使它们被注释为非空或原始。查询返回的列:[id,name,type,status]。
[id,name,type,status] 来自父类,而[startDate, endDate] 是子类字段。
在我的应用程序中的各种情况下,我只会列出队列名称、类型和状态,并且我希望将队列数据保存在一个表中以便快速查询。
我可以为各个子类创建子表并使用关系,但如果 Room 允许继承,所有这些都可以轻松避免。房间是否支持这种继承,如果是,我怎样才能让它工作?
谢谢。
【问题讨论】:
标签: android android-room