Je rencontre le problème indiqué dans le titre. Je les entités suivantes:Relation de plusieurs à plusieurs avec Room entraînant une erreur: Entités et Pojos doivent avoir un constructeur public utilisable
@Entity(tableName = "Person")
data class Person(@PrimaryKey var id: Int,
var firstName: String,
var surname: String,
var age: Int,
var numberOfHobbies: Int) {
@Ignore
constructor() : this(0, "", "", 0, 0)
}
@Entity(tableName = "Skill")
data class Skill(@PrimaryKey var id: Int,
var skillName: String) {
@Ignore
constructor() : this(0, "")
}
@Entity(tableName = "PersonSkill")
data class PersonSkill(var personId: Int,
var skillId: Int) {
@Ignore
constructor() : this(0, 0)
@field:PrimaryKey(autoGenerate = true)
var id: Int = 0
}
Et les relations suivantes:
data class SkillWithPersons(
@Embedded var skill: Skill = Skill(0, "UNKNOWN"),
@Relation(
parentColumn = "id",
entityColumn = "skillId",
entity = PersonSkill::class,
projection = arrayOf("personId")
) var personIds: List<Int> = emptyList()
) {
constructor() : this(Skill(0, "UNKNOWN"), emptyList())
}
data class PersonWithSkills(
@Embedded var person: Person = Person(0, "UNKNOWN", "UNKNOWN", 0, 0),
@Relation(
parentColumn = "id",
entityColumn = "personId",
entity = PersonSkill::class,
projection = arrayOf("skillId")
) var skillIds: List<Int> = emptyList()
) {
constructor(): this(Person(0, "UNKNOWN", "UNKNOWN", 0, 0), emptyList())
}
Et j'ai tout essayé, mais il ne fonctionne pas. Je continue à obtenir l'erreur suivante avec Kotlin-KAPT:
e: 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).
e:
e: Tried the following constructors but they failed to match:
e: Integer(int) : [value : null]
e: Integer(java.lang.String) : [s : null]
e: 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).
e:
e: java.lang.IllegalStateException:
J'utilise les versions suivantes:
Android Studio 3.0 avec Gradle 4, Chambre: 1.0.0-alpha9-1
, outils de construction: 26.0.2
, Kotlin: Il semble qu'il y ait un bug avec l'utilisation de @Relation
car kotin-kapt semble ne pas le gérer. Quelqu'un at-il déjà rencontré cette situation? J'ai même essayé d'enlever le projection
du @Relation
mais même cela ne semble pas faire la différence.
Avez-vous essayé le même schéma de pièce dans Java et le processeur d'annotation au lieu de kapt? –
Suppression du '@ Ignore' des constructeurs par défaut s'est débarrassé de l'erreur pour moi. – Virusman
@Virusman qui ne fonctionne pas pour moi – blackpanther