I have two data class Product
and ProductInfo
, ProductInfo
is an extention of Product
but how it should be writtein in Kotlin by using Room as if i try to use interface
instead data class
on Product i'm unable to set the @PrimaryKey
annotation?
@Entity(tableName = "articoli")
data class Articoli (
@PrimaryKey
var codiceArticolo: String,
var descrizione: String,
var prezzoVendita: Float,
var prezzoAcquisto: Float,
var unitaMisura: String,
)
data class InfoArticoli(
var codiceArticolo: String,
var descrizione: String,
var prezzoVendita: Float,
var prezzoAcquisto: Float,
var unitaMisura: String,
var giacenza: List<String>,
var famiglia: String,
var reparto: String,
var repartoCassa: String,
var scortaMinima: String,
var iva: String,
var fornitore: String,
)
CodePudding user response:
To answer to your first question "if i try to use interface instead data class on Product i'm unable to set the @PrimaryKey annotation?" Its no because Room use classes for store your data, the interfaces its only for define the behavior of some classes.
I not realy sure of what you do you mean with the word "extention" but i see two diferente approach of your problem.
First you can use the inheritance concept to etablish some behavior or some variable in your Product and define the other part in your ProductInfo
@Entity(tableName = "queue")
public class Queue {
int id;
String name;
int type;
int status;
}
@Entity(tableName = "production_queue")
public class ProductionQueue extends Queue {
...
}
The second approach its to use the foreign key to link two table of Room
@Entity(foreignKeys = arrayOf(ForeignKey(entity = ParentClass::class,
parentColumns = arrayOf("parentClassColumn"),
childColumns = arrayOf("childClassColumn"),
onDelete = ForeignKey.CASCADE)))
In this approach Product And InfoProduct are two distinct classes link with one value
CodePudding user response:
You can use the primaryKeys
parameter of the @Entity
annotation to specify specific primary keys.
I suspect that what you want is :-
data class Articoli (
var codiceArticolo: String,
var descrizione: String,
var prezzoVendita: Float,
var prezzoAcquisto: Float,
var unitaMisura: String,
)
@Entity(tableName = "articoli", primaryKeys = ["codiceArticolo"])
data class InfoArticoli(
@Embedded
var articoli: Articoli,
var giacenza: List<String>,
var famiglia: String,
var reparto: String,
var repartoCassa: String,
var scortaMinima: String,
var iva: String,
var fornitore: String,
)
- where @Embedded effectively extends the Articoli
- noting that you will have ongoing issues with giacenza as you either need a type converter or from a database perspective a table for the giacenza list.