Hello, I have a little problem with exposed and I ...
# exposed
b
Hello, I have a little problem with exposed and I don't understand where my mistake comes from, I'm not supposed to make a transaction here, and I have no idea where to add one to make it work.. . I'm still quite new to using the library, and speaking English quite badly, google is not the most optimal in translating documentation My error :
Copy code
java.lang.IllegalStateException: No transaction in context.
	at org.jetbrains.exposed.sql.transactions.TransactionManager$Companion.current(TransactionApi.kt:132)
	at org.jetbrains.exposed.sql.AbstractQuery.getTransaction(AbstractQuery.kt:9)
	at org.jetbrains.exposed.sql.AbstractQuery.iterator(AbstractQuery.kt:61)
	at kotlin.collections.CollectionsKt___CollectionsKt.firstOrNull(_Collections.kt:269)
	at org.jetbrains.exposed.dao.Entity.getReadValues(Entity.kt:45)
	at org.jetbrains.exposed.dao.Entity.lookup(Entity.kt:194)
	at org.jetbrains.exposed.dao.Entity.getValue(Entity.kt:174)
	at fr.ftnl.grupo.database.models.tbl.User.getDiscordUsername(Users.kt:25)
	at fr.ftnl.grupo.database.mediator.UsersMediator.getUserByDiscordId(UsersMediator.kt:21)
	at fr.ftnl.grupo.core.commands.list.buttons.JoinEvent.action(JoinEvent.kt:19)
	at fr.ftnl.grupo.core.commands.CommandManager.dispatch(CommandManager.kt:124)
	at fr.ftnl.grupo.core.listeners.Dispatcher.onEvent(Dispatcher.kt:21)
	at dev.minn.jda.ktx.events.CoroutineEventManager.runListener$suspendImpl(CoroutineEventManager.kt:101)
	at dev.minn.jda.ktx.events.CoroutineEventManager.runListener(CoroutineEventManager.kt)
	at dev.minn.jda.ktx.events.CoroutineEventManager$handle$1.invokeSuspend(CoroutineEventManager.kt:91)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
The class it tells me is wrong :
Copy code
package fr.ftnl.grupo.database.models.tbl

import fr.ftnl.grupo.database.models.tbj.UserGametag
import fr.ftnl.grupo.database.models.tbj.UserGametags
import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.jodatime.CurrentDateTime
import org.jetbrains.exposed.sql.jodatime.datetime
import org.joda.time.DateTime

object Users : IntIdTable("TBL_USERS_USR") {
    val discordId: Column<Long> = long("discord_id")
    val discordUsername: Column<String> = varchar("discord_username", 40)
    
    val createdAt: Column<DateTime> = datetime("created_at").defaultExpression(CurrentDateTime)
}

class User(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<User>(Users)
    
    var discordId by Users.discordId
    var discordUsername by Users.discordUsername
    
    val gametags by UserGametag referrersOn UserGametags.user
    
    val createdAt by Users.createdAt
}
The whole project is also available on github : https://github.com/OcelusPRO/grupo/tree/dev
s
You have to wrap
if (final.discordUsername != username)
in a transaction because
discordUsername
is lazy loaded.
b
Indeed, I had not seen this one, I had a second one at the level of a foreach loop but it has also been fixed, Thank you for your help. Just one last question, Is there a possibility to load the entirety of my object directly so as not to constantly need to have transactions each time I want to read one of the values?
s
There is an eager loading, but it only works inside the same transaction. https://github.com/JetBrains/Exposed/wiki/DAO#eager-loading
746 Views