Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

501
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda