Room支持实体继承吗? [英] Does Room support entity inheritance?

查看:358
本文介绍了Room支持实体继承吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我们的项目迁移到使用Room的方式,顺便说一句,我认为这是向前迈出的了不起的一步.

I am trying to migrate our project to use Room, which, by the way, I think is an awesome step forward.

我具有以下结构:

public class Entity extends BaseObservable {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "_id", typeAffinity = ColumnInfo.INTEGER) 
    private long mId;

    @ColumnInfo(name = "is_dirty")
    @TypeConverters(BooleanTypeConverter.class)
    private boolean mIsDirty;

    // default constructor and accessors omitted for brevity
}

@Entity(tableName = "some_entities")
public class SomeEntity extends Entity {

    @ColumnInfo(name = "type", typeAffinity = ColumnInfo.TEXT)        
    private String mType;

    @ColumnInfo(name = "timestamp", typeAffinity = ColumnInfo.INTEGER)
    private long mTimestamp;

    // constructor, accessors
}

当我尝试编译我的项目时,它失败,没有特定的错误.

When I try to compile my project, it fails with no specific error.

如果我尝试使用平面实体层次结构进行编译,一切都会很好.

If I try to compile it with a flat entity hierarchy, all is well.

所以,我的主要问题是: Room支持实体继承吗?能否从父类Entity中获取列定义?

So, my main question is: Does Room support entity inheritance? Will it be able to get the column definitions from the parent Entity class?

我还想知道扩展BaseObservable(我需要使数据绑定正常工作)是否会导致Room问题? BaseObservable具有一个私有瞬态字段,因此这可能会导致代码生成出现一些问题.

I would also like to know if extending BaseObservable (which I need to get the Data Binding working) can cause problems with Room? BaseObservable has one private transient field, so maybe this is causing some issues with the code generation.

是否有任何建议的模式可以解决此问题,还是只需要扁平化实体层次结构?

Are there any recommended patterns to deal with this, or will I just have to flatten my entity hierarchy?

推荐答案

经过进一步调查,结果显示房间实体不应该扩展BaseObservable类.它包含无法用@Ignore标记的字段并破坏了代码生成.

After further investigation it turns out that Room Entities should not extend the BaseObservable class. It contains fields that can't be marked with @Ignore and break the code generation.

房间在继承方面效果很好.注释将按预期方式处理,并且DB操作会正常运行.您可以从实体和POJO扩展.

Room works well with inheritance. The annotations are processed as expected and the DB operations behave normally. You can extend from both an Entity and a POJO.

这篇关于Room支持实体继承吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆