I’m using the DAO and I have an object with a one-...
# exposed
p
I’m using the DAO and I have an object with a one-to-many relationship. How can I load the list when I load that object? And if that’s not possible, when is it better to do more things within the transaction context or to open/close multiple transactions within short succession of each other?
t
parentEntity.children.toList()
?
p
I’m still getting
No transaction in context
errors when using the eager loading.
t
Could you share a piece of a code?
p
The
toList()
solution works, but weird, since I now have a separate list of children that are now separate from their parent
t
You just ask for it
How can I load the list when I load that object?
😄
p
True, but that list is (supposed to be) part of that object 😉
Copy code
val discordUser: DiscordUser? = transaction {
    DiscordUser.find { DiscordUsers.discordTag like "$name%" }.firstOrNull()?.load(DiscordUser::farmers)
}
This seems like it should work, but this gives me the
No transaction in context
error when I try to access
.farmers
outside of a transaction.
t
Sad, but all references are transaction-bound, so you can't call for it our of
transaction
block.
p
Alright, then I misunderstood what eager loading was about 🙂
t
It's an approach to prevent N+1 problem on loading multiple entities and their relations
p
I’m not familiar with that problem, but I can think of what that could mean. Thank you for your time and explanation. I’ll stick to using the
.toList()
method 🙂
👍 1