接下來我們來談談怎麼用 exposed DAO 處理資料之間的關係。
今天,先來談談一對多關係。
假設我們再多一個資料表 Users:
object Users : IntIdTable() {
    val name = varchar("name", 50).index()
    val city = reference("city", Cities)
}
class User(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<User>(Users)
    var name by Users.name
    var city by City referencedOn Users.city
}
City 類別則要加上 referrersOn:
class City(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<City>(Cities)
    var name by Cities.name
    val users by User referrersOn Users.city
}
利用 reference() 函式和 referrersOn,我們可以讓 Users 和之前的 Cities 相互綁定。
要建立資料時,我們可以這樣做:
val stPete = City.new {
    name = "St. Petersburg"
}
val munich = City.new {
    name = "Munich"
}
User.new {
    name = "Alice"
    city = stPete
}
User.new {
    name = "Bob"
    city = munich
}
println("Users in St. Petersburg: ${stPete.users.joinToString {it.name}}")