24

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

I have the following structure:

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.

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

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?

Danail Alexiev
  • 7,624
  • 3
  • 20
  • 28
  • So, I did a bit more testing and turns out that: YES, Room does support inheritance and YES, extending `BaseObservable` is a problem. I am currently removing the `BaseObservable` from the entity hierarchy. – Danail Alexiev Sep 20 '17 at 09:59

1 Answers1

17

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.

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.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Danail Alexiev
  • 7,624
  • 3
  • 20
  • 28
  • Does room support TypeConverters to be put on open class in kotlin? By any chance if you are aware – skygeek May 01 '18 at 11:53
  • 2
    Can you provide some example code snippets, how did you the setup to inherit an entity and query the extended entity from the db? – Dokumans May 28 '18 at 13:21
  • I am having issues extending objects, because the Dao validation uses the parent class instead of the child extended classes. – DeaMon1 Aug 22 '18 at 20:20
  • @DeaMon1, have you passed tableName value to Entity annotation to parent or child or both? – bojanb89 Sep 21 '18 at 22:06
  • Does this mean if you have your base class `Entity` and two classes that inherit from it, Room will be able to query an object using the BaseClass attributes (e.g. `id`) and then create the ChildClass entity of the correct type? – lucidbrot Apr 30 '19 at 05:23
  • 1
    @lucidbrot Yes. You can create a query that returns the child type and access the properties of the base class in the query clauses. – Danail Alexiev May 02 '19 at 13:30
  • @DanailAlexiev can I annotate the ParentClass as `@Entity(tableName="mytable")` or do I need a separate table for each ChildClass? I cannot get the first option to work (yet) – lucidbrot May 07 '19 at 05:15
  • @lucidbrot No, you shouldn't annotate the parent class with `@Entity`. If you're still trying to get this to work and have posted another question, I'll be more than happy to take a look. – Danail Alexiev May 16 '19 at 20:45
  • Thanks, @DanailAlexiev ! I asked the question [here](https://stackoverflow.com/q/56142162/2550406) - I have a solution that kinda works, but it feels like there must be a better/cleaner/easier way – lucidbrot May 17 '19 at 05:01