Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

502
Views
Exposed ORM: DSL vs DAO in Many-to many relationships best practices

I am setting up some many-to-many relationships and have so far been using the Exposed DSL pattern (as opposed to DAO). However, creating many-to-many relationships seem to only be possible using the DAO approach.

I know it is probably fine to use the two patterns interchangeably, but as I set up my project and move forward, I'm wondering what the best approach is from the perspective of code quality. Use them both or switch to DAO? Or the third option is that this question represents a misguided understanding of Kotlin and/or Exposed (new to both), in which case, where am I going wrong? Thanks in advance

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It is possible to use DSL to create many-to-many relationships for tables. However whether or not you should use DSL or DAO or both together would really be up to you and whether or not it makes the code easier or harder to read and maintain.

Here is a basic example of a many to many relationship with DSL.

import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction

object Users : IntIdTable() {
    val username = varchar("username", 50)
}

object Permissions : IntIdTable() {
    val name = varchar("name", 50)
}

object UserPermissionsJunctionTable : IntIdTable() {
    val user = reference("user", Users)
    val permission = reference("permission", Permissions)
}

fun main(args: Array<String>) {
    val db = Database.connect("jdbc:sqlite:test.db", "org.sqlite.JDBC")

    transaction {
        addLogger(StdOutSqlLogger)

        SchemaUtils.create(Users, Permissions, UserPermissionsJunctionTable)

        val userId = Users.insertAndGetId {
            it[username] = "john.smith"
        }

        val readPermissionId = Permissions.insertAndGetId {
            it[name] = "read"
        }

        val writePermissionId = Permissions.insertAndGetId {
            it[name] = "write"
        }

        UserPermissionsJunctionTable.insert {
            it[user] = userId
            it[permission] = readPermissionId
        }

        UserPermissionsJunctionTable.insert {
            it[user] = userId
            it[permission] = writePermissionId
        }

        val result = Users
            .join(UserPermissionsJunctionTable, JoinType.INNER, additionalConstraint = {
                Users.id eq UserPermissionsJunctionTable.user
        })
            .join(Permissions, JoinType.INNER, additionalConstraint = {
                UserPermissionsJunctionTable.permission eq Permissions.id
        })
            .slice(Users.username, Permissions.name).selectAll().map {
                it[Users.username] to it[Permissions.name]
        }

        println(result)
    }
}

This prints [(john.smith, read), (john.smith, write)]

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!