Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

330
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!