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

263
Views
android Room with kotlin value class?

I'm trying to use a room entity with a value class:

@JvmInline
value class UserToken(val token: String)

and the entity:

@Entity(tableName = TABLE_AUTH_TOKEN)
data class TokenEntity(
  @PrimaryKey val id: Int = 0,
  val token: UserToken
)

I get the following error:

error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
public final class TokenEntity {
             ^

is it even possible to use room with value class? I couldn't find anything about this. thanks

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I think yes if you can provide a type converter for it to change it to some sort of primitive data type (int , string, long ...etc) when it needs to be stored, and to change it back to its class type when it's fetched from database.

You can read about Type Converters from here

Referencing complex data using Room

other than that, your other class should be an entity and bind both your entities together using a Relation.

at least that's what I know about how to use Room.

over 4 years ago · Santiago Trujillo Report

0

UserToken always will have only one attribute? In this case, you don't need two classes, just use token: String directly on your entity class;

If you really need keep this class, you have two options:

  • TypeConverter, where you basically will convert the object into a json, and save as string in the database;

  • Relation, where you will transform the UserToken in a entity, and on TokenEntity save the tokenId.

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!