房间:相关实体 - 可用 public 构造函数
Room: related entities - usable public constructor
为了获得与 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,它确实有效。
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
}
}
我想知道为什么第一个(较快的)版本无法编译,因为它不像文档中显示的那样。
构造函数(数据)中所有变量的默认参数(正如我在其他 post 中看到的那样)class 似乎不是强制性的?
谢谢
数据class如何生成构造函数有几个主题。
由于您的构造函数中有一个可为空的对象,它将生成所有可能的构造函数。这意味着它生成
constructor(var subject: Subject)
constructor(var subject: Subject, var topics: List<Topic>)
有两种方法可以解决这个问题。第一个是预定义所有值,并使用所需的构造函数创建另一个忽略的构造函数。
data class SubjectView(
@Embedded
var subject: Subject,
@Relation(parentColumn = "idWeb", entityColumn = "subject_id", entity = Topic::class)
var topics: List<Topic> = ArrayList()
) {
@Ignore constructor(var subject: Subject) : this(subject, ArrayList())
}
另一种方法是创建半填充数据class,如
data class SubjectView(@Embedded var subject: Subject) {
@Relation var topics: List<Topic> = ArrayList()
}
注意第一个解决方案是正确的解决方案,您需要将@Ignore 设置为任何其他构造函数。
为了获得与 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,它确实有效。
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
}
}
我想知道为什么第一个(较快的)版本无法编译,因为它不像文档中显示的那样。
构造函数(数据)中所有变量的默认参数(正如我在其他 post 中看到的那样)class 似乎不是强制性的?
谢谢
数据class如何生成构造函数有几个主题。
由于您的构造函数中有一个可为空的对象,它将生成所有可能的构造函数。这意味着它生成
constructor(var subject: Subject)
constructor(var subject: Subject, var topics: List<Topic>)
有两种方法可以解决这个问题。第一个是预定义所有值,并使用所需的构造函数创建另一个忽略的构造函数。
data class SubjectView(
@Embedded
var subject: Subject,
@Relation(parentColumn = "idWeb", entityColumn = "subject_id", entity = Topic::class)
var topics: List<Topic> = ArrayList()
) {
@Ignore constructor(var subject: Subject) : this(subject, ArrayList())
}
另一种方法是创建半填充数据class,如
data class SubjectView(@Embedded var subject: Subject) {
@Relation var topics: List<Topic> = ArrayList()
}
注意第一个解决方案是正确的解决方案,您需要将@Ignore 设置为任何其他构造函数。