Can i extend a simple object class to create a Room database object?

Viewed 938

Let's say i have a simple object class:

public class Test {

    public String teststring;

}

But then i'd like to create another object class, and this time it would be a Room Database class:

@Entity(tableName = "test_table_one")
public class TestChildOne extends Test {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    public long test_id;

}

And then, i'd like to create another class:

@Entity(tableName = "test_table_two")
public class TestChildTwo extends Test {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    public long test_id;

}

The issue here would be that the String teststring of the parent class wouldn't have a @ColumnInfo annotation. Fact is, I don't want to set an @Entity annotation in the parent class, since those two child classes would be of different tables, as you can see.

So, can I just set a @ColumnInfo in the parent class without @Entity set? Would it work then when I create an object TestChildOne and set TestChildOne.teststring or would it throw an error?

Thank you very much in advance!

1 Answers

@ColumnInfo annotation will work even if it is in parent class ,so you can write @ColumnInfo even if class is not having @Entity annotation

public class Test {
     @ColumnInfo( name = "yourColumnName" )
    public String teststring;

}
Related