java - 与 Room 的多对多关系导致错误 : Entities and Pojos must have a usable public constructor

标签 java android kotlin android-room

我遇到了标题中所述的问题。我有以下实体:

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

我什么都试过了,还是不行。我不断收到 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:

我正在使用以下版本:

带有 Gradle 4 的 Android Studio 3.0, 房间:1.0.0-alpha9-1, 构建工具:26.0.2, Kotlin :1.1.51

使用 @Relation 似乎存在一个错误,因为 kotin-kapt 似乎无法处理它。有没有人遇到过这种情况?我什至尝试从 @Relation 中删除 projection,但即使那样似乎也没有什么不同。

最佳答案

“Florina Muntenescu”在她的博客 post 中提到,Room 中的@Relation 标签现在仅支持一对多关系。

同样针对上述问题,如下所述更改您的数据类

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

这样你就可以摆脱错误。

关于java - 与 Room 的多对多关系导致错误 : Entities and Pojos must have a usable public constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46730931/

相关文章:

kotlin - 如何使用 mockk 模拟 ObjectMapper.readValue()

android - 当在键盘上按下特定字符时,我可以切换到另一个 EditText 吗?

javascript - 如何从 s :hidden tag in Struts 2 获取 jQuery 中的对象列表

java - 解码 : com. google.android.gms.location.LocationResult 时找不到类

Android 忽略自签名证书

android - 如何将 Facebook 应用所有权迁移到商务管理平台?

Java/Kotlin Selenium Chromedriver 2Captcha

java - 使用 Quartz 立即执行一次性任务

java - 为什么zuul网关应用程序需要这么长时间才能以优雅的方式关闭

java - 如何使用 Logback 将 System.out 重定向到日志文件?