Return type for Android Room joins

Dao @Query(“SELECT * FROM Foo”) List<FooAndBar> findAllFooAndBar(); Class FooAndBar public class FooAndBar { @Embedded Foo foo; @Relation(parentColumn = “Foo.bar_id”, entityColumn = “Bar.id”) List<Bar> bar; // If we are sure it returns only one entry // Bar bar; //Getter and setter… } This solution seems to work, but I’m not very proud of it. What do … Read more

How to populate Android Room database table on first run?

Updated You can do this in 3 ways: important check this for migration details 1- Populate your database from exported asset schema Room.databaseBuilder(appContext, AppDatabase.class, “Sample.db”) .createFromAsset(“database/myapp.db”) .build(); 2- Populate your database from file Room.databaseBuilder(appContext, AppDatabase.class, “Sample.db”) .createFromFile(new File(“mypath”)) .build(); 3- You can run scripts after database is created or run every time database is opened … Read more

How can I represent a “many to many” relation with Android Room when column names are same?

I had a similar issue. Here is my solution. You can use an extra entity (ReservationGuest) which keeps the relation between Guest and Reservation. @Entity data class Guest( @PrimaryKey val id: Long, val name: String, val email: String ) @Entity data class Reservation( @PrimaryKey val id: Long, val table: String ) @Entity data class ReservationGuest( … Read more

Android room persistent library – how to insert class that has a List object field

You can easly insert the class with list object field using TypeConverter and GSON, public class DataConverter { @TypeConverter public String fromCountryLangList(List<CountryLang> countryLang) { if (countryLang == null) { return (null); } Gson gson = new Gson(); Type type = new TypeToken<List<CountryLang>>() {}.getType(); String json = gson.toJson(countryLang, type); return json; } @TypeConverter public List<CountryLang> toCountryLangList(String … Read more

Room – LiveData observer does not trigger when database is updated

I had a similar problem using Dagger 2 that was caused by having different instances of the Dao, one for updating/inserting data, and a different instance providing the LiveData for observing. Once I configured Dagger to manage a singleton instance of the Dao, then I could insert data in the background (in my case in … Read more

How to dynamically query the room database at runtime?

Room supports @RawQuery annotation to construct queries at run-time. Step 1 : Make DAO method Mark the DAO method with @RawQuery annotation instead of normal @Query. @Dao interface BooksDao{ @RawQuery List<Book> getBooks(SupportSQLiteQuery query); } Step 2 : Construct the query Room uses prepared statements for security and compile time verification. Therefore, while constructing queries, we … Read more

Room – Schema export directory is not provided to the annotation processor so we cannot export the schema

In the build.gradle file for your app module, add this to the defaultConfig section (under the android section). This will write out the schema to a schemas subfolder of your project folder. javaCompileOptions { annotationProcessorOptions { arguments += [“room.schemaLocation”: “$projectDir/schemas”.toString()] } } Like this: // … android { // … (compileSdkVersion, buildToolsVersion, etc) defaultConfig { … Read more