How to add new tables in library with Room?

11,785

Solution 1

@Database(version = 1, entities = {User.class, Book.class})
abstract class AppDatabase extends RoomDatabase {
     // BookDao is a class annotated with @Dao.
     abstract public BookDao bookDao();
     // UserDao is a class annotated with @Dao.
     abstract public UserDao userDao();
     // UserBookDao is a class annotated with @Dao.
     abstract public UserBookDao userBookDao();
}

If you want to update the Room database and add table...just add another entity then update the version if you add another entity like Movies table.. do something like this

@Database(version = 2, entities = {User.class, Book.class, Movies.class})
abstract class AppDatabase extends RoomDatabase {
     // BookDao is a class annotated with @Dao.
     abstract public BookDao bookDao();
     // UserDao is a class annotated with @Dao.
     abstract public UserDao userDao();
     // UserBookDao is a class annotated with @Dao.
     abstract public UserBookDao userBookDao();
     // MoviesDao is a class annotated with @Dao.
     abstract public MoviesDao moviesDao();
     // UserMoviesDao is a class annotated with @Dao.
     abstract public UserMoviesDao userMoviesDao();
}

for reference you can check ... this

Solution 2

The Room persistence library supports incremental migrations with the Migration classes to address this need. Each Migration subclass defines a migration path between a startVersion and an endVersion.

So, the right answer (and the correct way because you should not use fallbackToDestructiveMigration) is :

Add your new table as a java class

@Entity(tableName = "Fruit")
public class Fruit implements Parcelable {
        
        @PrimaryKey(autoGenerate = true)
        @ColumnInfo(index = true, name = "id")
        private int id;
    
        @ColumnInfo(name = "name")
        private String name;
    
    ...
    
    }

Update your database version AND add your entity in entities declaration (you add the class definition to tells room should take the class into consideration) and add your dao getter

@Database(version = 2, entities = {User.class, Fruit.class})
abstract class AppDatabase extends RoomDatabase {
     
     abstract public UserDao userDao();
     
     abstract public FruitDao fruitDao();
}

Add migration sql script with your database builder like this

 public static final Migration MIGRATION_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            database.execSQL("CREATE TABLE `Fruit` (`id` INTEGER, "
                    + "`name` TEXT, PRIMARY KEY(`id`))");
        }
    };
    
    Room.databaseBuilder(getApplicationContext(), MyDb.class, "database-name")
            .addMigrations(MIGRATION_1_2).build();

Source : https://developer.android.com/training/data-storage/room/migrating-db-versions

Share:
11,785

Related videos on Youtube

Eylen
Author by

Eylen

SOreadytohelp

Updated on June 04, 2022

Comments

  • Eylen
    Eylen about 2 years

    I am planning to start the migration of an existing app to Architecture Components and one of my doubts is how should I organize the new code.

    I have some tables that are added in a personal library that's only included in some flavors, how can those Entities and DAOs be added to the main application if the database class exists on the main application?

    Should I add another database class to the library? If so, wouldn't it collide with existing Database class in the main application?

    I have been searching, but haven't been able to find any example or tutorial...

    Edit to clarify Database question

    From the docs I understand that in the Database abstract class, you have to tell wich Entities exists and also create access methods for the DAOs. How could this be done if there are entities in the library?

    @Database(entities = {User.class}, version = 1)
    public abstract class AppDatabase extends RoomDatabase {
        public abstract UserDao userDao();
    }
    
  • Eylen
    Eylen over 6 years
    Yes, but the problem is that the Database class is in the application and the new entity is in a separate library that won't be included in all applications, it would just be included in some flavors.... Maybe adding a database per flavor could do the trick?
  • Arda Kaplan
    Arda Kaplan about 4 years
    This is not about the question!
  • Santanu Sur
    Santanu Sur about 4 years
    @Arda why dont u put up the perfect answer !
  • Arda Kaplan
    Arda Kaplan about 4 years
    @SantanuSur you are misunderstood, question is not "How to add new class to room database?" Question is how to add LIBRARY CLASS TABLE. You can not add "Entity" annotation to lib classes. You can not reach them. All of yours can not understand the question
  • MaxV
    MaxV over 3 years
    Hi @rumbur4k. Usually answers with detailed explanation obtains more upvotes. Would you like to add a few words?
  • Aqeel Mughal
    Aqeel Mughal almost 3 years
    Thank you So much. your answer made my day... 😊😊😊