How can I update a TypeConverted column of an entity in Room Dao function
Asked Answered
U

2

8

I have an @Entity which holds a variable(list of custom object) along with other fields for the table. I am able to insert, fetch and delete from this entity.

But I am facing an issue in updating the entity:

I want to update that particular field which holds a list of custom object in the table but while compilation it throws an error:

error: Query method parameters should either be a type that can be converted into a
database column or a List / Array that contains such type. You can consider adding a Type Adapter for this.

I could update the complete row object but the problem lies in updating this single field. I am using TypeConverters on my @Database class but I have tried using them on Dao and the update function itself but it reports the same error.

Can someone please help me to update this particular field in the row, I don't want to provide the full object of this entity for this to happen.

My Entity is:

@Entity data class TableName(
    @PrimaryKey
    var id: String = "",
    @SerializedName("varOne")
    @Expose
    var varOne: List<CustomObjects>? = null)

Update method is something like this:

@TypeConverters(MyTypeConverters.VarOneListTypeConverters::class)
@Query("Update TableName SET varOne = :varOneList")
abstract fun updateTableName(varOneList: List<CustomObjects>)
Unmade answered 5/10, 2018 at 9:48 Comment(3)
Did you find any solution? I am facing the same issue.Deiform
Also same for me. Room seems a amazing LIB, but I've been struggling in silly things like that.Minsk
Can you post your VarOneListTypeConverters code?Hysterectomize
B
2

Ideally you should try to model this as a relationship with a separate table for the CustomObject and a foreign key ref to the primary key of TableName. However you can still write a converter for the type List<CustomObject>. Room only understands the Sqlite Data types and any other type needs to be converted to one of they types that room understands. They have provided the TypeConverter annotations for the same. If you use Gson to serialize your CustomObject then you can use the following converter. The code is self explanatory

public class Converters {
   @TypeConverter
   public static ArrayList<String> fromString(String value) {
      Type listType = new TypeToken<ArrayList<CustomObject>>() {}.getType();
      return new Gson().fromJson(value, listType);
   }
   @TypeConverter
   public static String fromArrayList(ArrayList<CustomObject> list) {
      Gson gson = new Gson();
      String json = gson.toJson(list);
      return json;
   }
}

And you just have to add this converter to your Database class

@TypeConverters(Converters::class) 
abstract class YourDatabase extends RoomDatabase
Baillieu answered 8/10, 2019 at 6:40 Comment(0)
L
0

You can directly insert the string. Typeconverter doesn't seem to be working with Update query.

@Query("Update TableName SET varOne = :varOneList")
abstract fun updateTableName(varOneList: String)

When you call this method do the conversion manually

tableDao.updateTableName(Gson().toJson(varOneList))

Here I'm using Gson to do the conversion, you can use whatever you are using in your converter class.

Ideally, you should structure your tables such that you don't have to insert a List as String

Lightish answered 28/1, 2022 at 18:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.