Skip to content

Error: Entities and Pojos must have a usable public constructor error occurs with Relations. #434

@rs146

Description

@rs146

I am encountering the issue stated in the title. I have the following entities:

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

And the following relationships:

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

And I have tried everything, and yet it does not work. I keep getting the following error with 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:

I am using the following versions:

Android Studio 3.0 with Gradle 4,
Room: 1.0.0-alpha9-1,
Build tools: 26.0.2,
Kotlin: 1.1.51

It seems there's a bug with the use of @Relation as kotin-kapt seems to no handle it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions