Android room entity, how to handle static feilds?

Viewed 78

Do we need to annotate static fields with @ignore in room?

@Entity(tableName = "table_users")
public class User {
    private static final String MY_CONSTANT = "whatever";

    @PrimaryKey(autoGenerate = true)
    public int id;

    public String name;
    public int age;
}

I read the documentation but did not find anything about this.

1 Answers

Do we need to annotate static fields with @ignore in room?

No, they are automatically ignored.

You can see this by making the project and then inspecting the generated java for the database (database class name suffixed with _Impl). The table will be defined in the createAllTables method.

For example the following :-

@Entity(tableName = "user")
public class User {

    private static final String MY_CONSTANT = "whatever";

    @PrimaryKey
    Long id;
    String userName;
    String userEmail;
    String userPassword;

    public User(){};

    @Ignore
    public User(String userName, String userEmail, String userPassword) {
        this.userName = userName;
        this.userEmail = userEmail;
        this.userPassword = userPassword;
    }
}

Results in :-

_db.execSQL("CREATE TABLE IF NOT EXISTS `user` (`id` INTEGER, `userName` TEXT, `userEmail` TEXT, `userPassword` TEXT, PRIMARY KEY(`id`))");

i.e. there is no MY_CONSTANT column.

Related