1

I'm trying to make a Room entity by using @Ignore to a list but when i try to build the app i get the following error:

 Entities and POJOs must have a usable public constructor

Here is how my code looks like:

@Entity(tableName = "prodotti_table")
data class Prodotto(
    @PrimaryKey(autoGenerate = false)
    var codart: String,
    var desc: String,
    var prezzo_acq: Float,
    var prezzo_vend: Float,
    @Ignore var barcode: List<Barcode>,
    var qta: Float
)

@Entity(tableName = "barcode_prodotti_table")
data class Barcode(
    @PrimaryKey
    var id: Int,
    var codart: String,
    var barcode: String,
    var qta: Float
)

How can i solve it still by using the @Ignore in that data class?

Kasper Juner
  • 832
  • 3
  • 15
  • 34

1 Answers1

1

You can't put an ignored property in the constructor, so do this:

@Entity(tableName = "prodotti_table")
data class Prodotto(
    @PrimaryKey(autoGenerate = false)
    var codart: String,
    var desc: String,
    var prezzo_acq: Float,
    var prezzo_vend: Float,
    var qta: Float
) {
    @Ignore var barcode: List<Barcode> = emptyList()
}

But remember that a property that isn't in the constructor of a data class will not participate in equals, hashcode, copy, or toString.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154