2017-10-13 21 views
3

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.

+0

Avez-vous essayé le même schéma de pièce dans Java et le processeur d'annotation au lieu de kapt? –

+0

Suppression du '@ Ignore' des constructeurs par défaut s'est débarrassé de l'erreur pour moi. – Virusman

+0

@Virusman qui ne fonctionne pas pour moi – blackpanther

Répondre

0

@Relation Tag dans la chambre soutenir en ce moment seul à navire Beaucoup de relation comme mentionné par « Florina Muntenescu » dans son blog post

également pour la question ci-dessus faire des changements dans vos classes de données comme mentionné ci-dessous

@Entity(tableName = "Person") 
data class Person(@PrimaryKey 
        var id: Int = 0, 
        var firstName: String = "", 
        var surname: String = "", 
        var age: Int = 0, 
        var numberOfHobbies: Int = 0) 

@Entity(tableName = "Skill") 
data class Skill(@PrimaryKey 
       var id: Int = 0, 
       var skillName: String = "") 

@Entity(tableName = "PersonSkill") 
data class PersonSkill(@PrimaryKey 
         var id: Int = 0 
         var personId: Int = 0, 
         var skillId: Int = 0) 



data class SkillWithPersons(
    @Embedded 
    var skill: Skill? = null 
    @Relation(
      parentColumn = "id", 
      entityColumn = "skillId", 
      entity = PersonSkill::class, 
      projection = arrayOf("personId")) 
      var personIds: List<Int> = ArrayList() 
      ) 

data class PersonWithSkills(
    @Embedded 
    var person: Person? = null 
    @Relation(
      parentColumn = "id", 
      entityColumn = "personId", 
      entity = PersonSkill::class, 
      projection = arrayOf("skillId")) 
      var skillIds: List<Int> = ArrayList() 
) 

De cette façon, vous pouvez vous débarrasser des erreurs.