Error: Entities and Pojos must have a usable public constructor error occurs with Relations.
See original GitHub issueI 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:
- Created 6 years ago
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
(orInteger
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 returnList<PersonSkill>
instead ofList<Int>
for the element annotated with@Relation
.“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.