0

I am using Entity class as model for API response it has List<Address> field I want it at serialization phase but i want to ignore it from stored in database as it will need TypeConverter and I don't want this.


@Entity(tableName = "USER")
data class User(
    @Ignore
    @SerializedName("address")
    val address: List<Address>?,
    @SerializedName("auth_token")
    @ColumnInfo(name = "authToken")
    val authToken: String?)

I have used @Ignore but got this 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 User {
             ^

Intelli J

Hint: I did not want to store this filed at db

thmspl
  • 2,437
  • 3
  • 22
  • 48
Mahmoud Mabrok
  • 1,362
  • 16
  • 24

2 Answers2

0

i guess the problem is that you don't specify a default value for variable address. Try with this:

@Entity(tableName = "USER")
data class User(
  @SerializedName("auth_token")
  @ColumnInfo(name = "authToken")
  val authToken: String?
  @Ignore
  @SerializedName("address")
  val address: List<Address>? = null,
  )
Pierluigi
  • 425
  • 2
  • 6
0

The error is telling you what is wrong:

You can have [...] a constructor whose parameters match the fields (by name and type)

Room has to know, how to construct your object from the database. When you use @Ignore on a parameter it is not listed as field. That's why parameters and fields don't match.

Therefor, you should not put @Ignored fields in the constructor. Try doing the following:

@Entity(tableName = "USER")
data class User(
    @SerializedName("auth_token")
    @ColumnInfo(name = "authToken")
    val authToken: String?) {

    @Ignore
    @SerializedName("address")
    val address: List<Address>? = null

}

nulldroid
  • 1,150
  • 8
  • 15