Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

332
Vistas
TypeConverters not working for Collections in @Query

I've an entity called Events which is defined as follows:

@Entity(tableName = "Events")
data class Event(@PrimaryKey val id: Long, val name: String, val venues: Set<String>, val rating: Int)

I've a pair of @TypeConverter methods for handling Set<String>:

@TypeConverter
fun fromStringToStringSet(str: String): Set<String> = str.split("<|>")

@TypeConverter
fun fromStringSetToString(set: Set<String>): String = set.joinToString("<|>")

In my Dao, I've a method annotated with @Query as follows:

@Query("UPDATE Events SET name = :name, venues = :venues WHERE id = :id")
fun updateAndRetainRating(id: Long, name: String, venues: Set<String>)

When I try to update an event with 2 venues, I get a runtime error telling me that the SQL couldn't be compiled. The generated SQL is:

UPDATE Events SET name = ?, venues = ?,? WHERE id = ?

This is obviously wrong. Looking into the generated code, Room is getting the size of Set<String> and adding the same number of ?s.

Why isn't my TypeConverter being used? I don't face this issue in other queries(@Insert and @Query(for SELECT)). Other TypeConverters are also working fine.

EDIT: The same issue also occurs if I use List<String> + TypeConverters instead of Set<String>.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Looking into the Room documentation, it appears that whenever we use a Collection in @Query, it bypasses TypeConverters and is straight away flattened.

For example, this:

@Query("SELECT * FROM Customers WHERE city IN (:cities)")
fun getCustomersInCities(cities: List<String>): Flowable<List<Customer>>

results in SELECT * FROM Customers WHERE city IN ('london', 'paris') if cities contains "london" and "paris".

Unfortunately, the following is also converted in a similar way:

@Query("UPDATE Customers SET phoneNumbers = :phoneNumbers WHERE id = :id")
fun updateCustomerPhoneNumbers(id: Long, phoneNumbers: List<String>)

The result is UPDATE Customers SET phoneNumbers = 1234567890, 9876543210 WHERE id = 23 if id is 23 and the phoneNumbers contains "1234567890" and "9876543210".

While this does make sense, it is really inconvenient and should be documented more clearly.

over 4 years ago · Santiago Trujillo Denunciar

0

This solution worked for me: TypeConverter not working when updating List<Boolean> in Room Database

Basically, replace List<String> by ArrayList<String>.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda