I set up a many-to-many relationship just like the...
# exposed
d
I set up a many-to-many relationship just like they descriped it in the documentation (https://github.com/JetBrains/Exposed/wiki/DAO#many-to-many-reference) But when I call
Entity.getAll()
I still get an
No transaction in context.
Error! I created each entity in an own transaction. Like described in the documentation! Any hints where to look at?
a
show us some code (edit in thread (;)
d
Sure, just a second
Is a link to the files at github ok?
a
ofc
d
Here is my user model https://github.com/dennisschroeder/directus_api/blob/master/src/domain/model/User.kt Here the role model: https://github.com/dennisschroeder/directus_api/blob/master/src/domain/model/Role.kt Thats where I initially setup the first admin user and the adminstration role and link them together: https://github.com/dennisschroeder/directus_api/blob/master/src/Bootstrap.kt See Line 81 and below! Here I put all to gehter and try to get the roles with the users attached: https://github.com/dennisschroeder/directus_api/blob/master/src/endpoints/role/Roles.kt Line 63 and below
a
@Dennis Schröder where does it throw?
Here in line 64
a
what is
asyncTransaction
?
also full exception?
are you sure you are in a transaction?
try explicitly wrapping your repository call in
transaction
d
Yes!
a
It actually fails on
Role.getUsers
d
Copy code
@JsonAdapter(ExposedTypeAdapter::class)
@ExposedGSON.JsonDatabaseIdField("id")
open class Role(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<Role>(Roles)

    var name by Roles.name
    var description by Roles.description
    var ipWhitelist by Roles.ipWhitelist
    var navBlacklist by Roles.navBlacklist
    var externalId by Roles.externalId

    var users by User via UserRoles
}
Yeah I saw that too! But it´s like in the documentation!
The call on
Role.getUsers
is made from exposed! I have no idea how I can wrap it in another
transaction
!
explicitly wrapping the repository call in
transaction
didn´t solve the problem either! Tried it!
OK! I think I could narrow it down a bit!
Role.all().toList().first().users.first().email
When I do it like this direct inside an transaction, everything works fine. So I guess ist the lazy loading mechnism, that calls the
getUsers
somwhere in the serializing process after I called
call.respond(entities)
!
And there it is outside the transaction!
t
What
ExposedTypeAdapter
should returns? Every entity field or only ids? As you may know
text/blob
fields are lazy loaded by jdbc drivers, so it looks like when you are trying to serialize
ipWhitelist/navBlacklist
inside a
launch
block Gson is trying to load text values from already closed transaction.
d
Yeah I figured it out how to get it right. But actually it was the many-to-many relation that is lazy loaded and therefore had some issue! Just needed to rearrange my code a bit. Thanks for your answer.
t
d
Yes I did! But I found the problem! I am using exposed in combination with ktor. Therfore I send my database actions to anoterh thread with
withContext(<http://Dispatcher.IO|Dispatcher.IO>) {query()}
. And it seems that then I loose my transaction context.