Thread
#exposed
    b

    bubakovsky

    4 years ago
    Hi! I'm a bit new to ORM and DB connection in app. I've switched from DAO to DSL way because of better separation between data and code (multitier architecture). Question - what would you recommend to access values from referenced columns (like in DAO way)? I'm talking about using
    data 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

    tapac

    4 years ago
    Why 1:N is a
    Map
    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)
            }
        }
    It might be not very clear, sorry. I mostly use Exposed DAO for entities.