https://kotlinlang.org logo
#exposed
Title
# exposed
p

pindab0ter

05/15/2019, 11:50 AM
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

tapac

05/15/2019, 12:12 PM
parentEntity.children.toList()
?
p

pindab0ter

05/15/2019, 12:56 PM
I’m still getting
No transaction in context
errors when using the eager loading.
t

tapac

05/15/2019, 12:56 PM
Could you share a piece of a code?
p

pindab0ter

05/15/2019, 12:56 PM
The
toList()
solution works, but weird, since I now have a separate list of children that are now separate from their parent
t

tapac

05/15/2019, 12:57 PM
You just ask for it
How can I load the list when I load that object?
😄
p

pindab0ter

05/15/2019, 12:58 PM
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

tapac

05/15/2019, 1:00 PM
Sad, but all references are transaction-bound, so you can't call for it our of
transaction
block.
p

pindab0ter

05/15/2019, 1:01 PM
Alright, then I misunderstood what eager loading was about 🙂
t

tapac

05/15/2019, 1:01 PM
It's an approach to prevent N+1 problem on loading multiple entities and their relations
p

pindab0ter

05/15/2019, 1:02 PM
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
9 Views