2017-10-13 24 views
3

我遇到標題中所述的問題。我有以下實體:與房間的多對多關係導致錯誤:實體和Pojos必須具有可用的公共構造函數

@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 
} 

而以下關係:

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()) 
} 

我已經嘗試了一切,但它不工作。我不斷收到與科特林-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: 

我使用以下版本:

過Android Studio 3.0搖籃4, 客房:1.0.0-alpha9-1, 編譯工具:26.0.2, 科特林: 1.1.51

似乎有一個錯誤使用@Relation因爲kotin-kapt似乎沒有處理它。有沒有人遇到過這種情況?我甚至試圖從@Relation中刪除projection,但即使這似乎沒有什麼區別。

+0

您是否嘗試過在Java和註釋處理器中使用相同的Room模式而不是kapt? –

+0

從默認構造函數中刪除'@ Ignore'爲我刪除了錯誤。 – Virusman

+0

@Virusman不適合我 – blackpanther

回答

0

@Relation標籤在房間右邊由「弗洛里納Muntenescu」在她的博客post

也提到了上述問題,使你的數據類更改下方

提到現在只支持一對多的關係船
@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() 
) 

這樣你就可以擺脫錯誤。

相關問題