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)]