2

Its easy to create Entity and corresponding table in room. Assume we have a User entity.

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

@PrimaryKey
@ColumnInfo(name = "userid")
@NonNull
private String mId;

@ColumnInfo(name = "username")
private String mUserName;

@ColumnInfo(name = "last_update")
private Date mDate;

The data will be stored in table "users" by default. Can I keep the same entity but having 2 tables at the same time? E.g. "old_users" and "users"

gabin
  • 927
  • 1
  • 10
  • 20
  • 1
    I think [this](https://stackoverflow.com/questions/48279481/multiple-tables-with-same-type-of-objects-in-room-database/48864379#48864379) will be helpful. – Bertram Gilfoyle Jul 18 '18 at 15:48

1 Answers1

4

Not directly. Entities have a 1:1 mapping to tables.

You could use Java inheritance to minimize code duplication and support multiple tables (e.g., User and OldUser inheriting from BaseUser).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • What if someone's using data class on Kotlin where all fields are passed through the main constructor and the class has no extra methods, how can we minimize duplication ? – Tamim Attafi Jul 12 '19 at 05:44
  • @TamimAttafi: Minimize *what* duplication? I suggest that you ask a separate Stack Overflow question where you can explain in greater detail what you are looking for. To be honest, while my answer is technically correct, IMHO the better answer is "do not have an `old_users` table -- have an `isOld` property on `User`". – CommonsWare Jul 12 '19 at 10:51