How to decouple room entity

时间:2019-04-08 13:12:42

标签: android android-room

I have room entity like this

@Entity(tableName = "AppUser",
        indices = {@Index(value = {"UserId", "UserName"}, unique = true)})
public class AppUser  {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "UserId")
    public int id;

    public AppUser(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

}

What I want to do is to later add additional column to existing table by extending AppUser class . Something like following

public class ExtendedUser extends AppUser {

    public ExtendedUser(int id) {
        super(id);
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @ColumnInfo(name = "address")
    String address;
}

So later if someone wants to modify the table , they do not need to modify AppUser , rather they would simply extend it .

Can I achieve it ?

0 个答案:

没有答案