【发布时间】:2017-10-26 13:47:30
【问题描述】:
为了获得与 Room 的 OneToMany 关系,我创建了一个带有 @Embedded 对象和 @Relation 变量的 POJO。
data class SubjectView(
@Embedded
var subject: Subject,
@Relation(parentColumn = "idWeb", entityColumn = "subject_id", entity = Topic::class)
var topics: List<Topic>?
)
但是在编译时出现这个错误
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)
[...]
Tried the following constructors but they failed to match:
SubjectView(biz.eventually.atpl.data.db.Subject,java.util.List<biz.eventually.atpl.data.db.Topic>) : [subject : subject, topics : null]
嗯,构造函数 [subject : subject, topics : null] 看起来不错???
但是,如果我使用无参数构造函数和全参数构造函数更改我的类,它确实有效。
class SubjectView() {
@Embedded
var subject: Subject = Subject(-1, -1, "")
@Relation(parentColumn = "idWeb", entityColumn = "subject_id", entity = Topic::class)
var topics: List<Topic>? = null
constructor(subject: Subject, topics: List<Topic>?) : this() {
this.subject = subject
this.topics = topics
}
}
我想知道为什么第一个(更快的)版本无法编译,因为它不像文档显示的那样。
构造函数(数据)类中所有变量的默认参数(正如我在其他帖子中看到的那样)似乎不是强制性的?
谢谢
【问题讨论】:
标签: android kotlin entities android-room android-architecture-components