我遇到标题中所述的问题。我有以下实体:
@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
,
科特林:1.1.51
使用@Relation
似乎存在一个错误,因为kotin-kapt似乎没有处理它。有没有人遇到过这种情况?我甚至试图从projection
中移除@Relation
,但即使这样做似乎没有什么区别。
答案 0 :(得分:0)
同样对于上述问题,请在下面提到的数据类中进行更改
@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()
)
这样你就可以摆脱错误。