question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
florina-muntenescucommented, Oct 13, 2017

Thanks @rs146 for reporting this. The problem here is that Room can’t find the right constructor for the elements in the relation, when the element is Int (or Integer in Java). I created an issue on our internal tracker for this but chances are this won’t be fixed for the 1.0 release, unfortunately. For now, the workaround is to return List<PersonSkill> instead of List<Int> for the element annotated with @Relation.

3reactions
florina-muntenescucommented, Oct 17, 2017

“Note that the @Relation annotated field cannot be a constructor parameter, it must be public or have a public setter.” - from docs Room first constructs the object and then sets the data.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error:Entities and Pojos must have a usable public constructor ...
Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields...
Read more >
Error:Entities and Pojos must have a usable public ... - GitHub
Error :Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the...
Read more >
Android : Room Persistence: Error:Entities and Pojos must ...
Android : Room Persistence: Error : Entities and Pojos must have a usable public constructor [ Beautify Your Computer ...
Read more >
How to fix Entities and POJOs must have a usable public ...
The general case: data class MyDataClass( var myfield: String ){ constructor(myfield: String): this(myfield) }. For your data classes: Just provide a ...
Read more >
entities and pojos must have a usable public constructor java
Error :Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found