Я встречаю вопрос, указанный в названии. У меня есть следующие объекты:
@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:
Я использую следующие версии:
Android Studio 3.0 с Gradle 4, Room: 1.0.0-alpha9-1
, Build tools: 26.0.2
, Kotlin: 1.1.51
Кажется, есть ошибка с использованием @Relation
поскольку kotin-kapt, похоже, не справляется с этим. Кто-нибудь сталкивался с этой ситуацией раньше? Я даже попытался удалить projection
из @Relation
но даже это, похоже, не имеет значения.