bubakovsky
07/08/2018, 5:46 PMdata class
in higher tiers. Only possible way I could come up with is to create Map
for every 1:N relationship, which might be very monstrous in the end. How do you usually deal with that? Thanks for the reply in advance!tapac
07/11/2018, 10:42 AMMap
not a List
?
You may end up like this :
object ParentTable : IntIdTable()
object ChildTable : IntIdTable() {
val parentId = reference("parentId", ParentTable)
}
data class Parent(val id: Int) {
val children get() = ChildTable.select { ChildTable.parentId eq id }.map { Child.wrap(it) }
companion object {
fun wrap(rs: ResultRow) : Parent = Parent(rs[ParentTable.id].value)
}
}
data class Child(val id: Int, val parentId: Int) {
val parent get() = ParentTable.select { ParentTable.id eq parentId }.singleOrNull()?.let { Parent.wrap(it) }
companion object {
fun wrap(rs: ResultRow) : Child = Child(rs[ChildTable.id].value, rs[ChildTable.parentId].value)
}
}