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

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 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

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 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