Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

341
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda