Android room persistent library – TypeConverter error of error: Cannot figure out how to save field to database”

This is a common problem I’ve seen since Room was announced. Room does not support the ability to store Lists directly, nor the ability to convert to/from Lists. It supports converting and storing POJO’s. In this case the solution is simple. Instead of storing a List<CountryLang> you want to store CountryLangs (note the ‘s’) I’ve … Read more

How to use Room Persistence Library with pre-populated database?

This is how I solved it, and how you can ship your application with a pre-populated database (up to Room v. alpha5) put your SQLite DB database_name.db into the assets/databases folder take the files from this repo and put them in a package called i.e. sqlAsset in your AppDatabase class, modify your Room’s DB creation … Read more

Room Persistence: Error:Entities and Pojos must have a usable public constructor

It’s not a problem in your case, but for others, this error can occur if you have @Ignore params in your primary constructor, i.e. Room expects to have either: parameterless constructor or constructor with all fields not marked with @Ignore for example: @Entity(tableName = “movies”) data class MovieKt( @PrimaryKey var id : Int, var title: … Read more

Android Room: Insert relation entities using Room

You can do this by changing your Dao from an interface to an abstract class. @Dao public abstract class UserDao { public void insertPetsForUser(User user, List<Pet> pets){ for(Pet pet : pets){ pet.setUserId(user.getId()); } _insertAll(pets); } @Insert abstract void _insertAll(List<Pet> pets); //this could go in a PetDao instead… @Insert public abstract void insertUser(User user); @Query(“SELECT * … Read more

Android room persistent: AppDatabase_Impl does not exist

For those working with Kotlin, try changing annotationProcessor to kapt in the apps build.gradle for example: // Extensions = ViewModel + LiveData implementation “android.arch.lifecycle:extensions:1.1.0” kapt “android.arch.lifecycle:compiler:1.1.0” // Room implementation “android.arch.persistence.room:runtime:1.0.0” kapt “android.arch.persistence.room:compiler:1.0.0” also remember to add this plugin apply plugin: ‘kotlin-kapt’ to the top of the app level build.gradle file and do a clean and … Read more

Android Room Database: How to handle Arraylist in an Entity?

Type Converter are made specifically for that. In your case, you can use code snippet given below to store data in DB. public class Converters { @TypeConverter public static ArrayList<String> fromString(String value) { Type listType = new TypeToken<ArrayList<String>>() {}.getType(); return new Gson().fromJson(value, listType); } @TypeConverter public static String fromArrayList(ArrayList<String> list) { Gson gson = new … Read more

Android Persistence room: “Cannot figure out how to read this field from a cursor”

Document is really confusing. Try with just below classes: 1) User Entity: @Entity public class User { @PrimaryKey public int id; // User id } 2) Pet Entity: @Entity public class Pet { @PrimaryKey public int id; // Pet id public int userId; // User id public String name; } 3) UserWithPets POJO: // Note: … Read more