While this isn’t strictly a Kotlin Spring question...
# spring
j
While this isn’t strictly a Kotlin Spring question I wonder if anyone knows this: I have a JPA entity that has a lazy relationship. There is one method where I’d like it to be fetched eagerly. In Django for example it’s lazy by default but you can add
select_related
to make it eager. Is there a way to load a lazy relationship eagerly in Spring JPA without having to do
entities.forEach { it.lazy_loaded_attribute }
or something?
n
@Jilles Soeters the answer is: you should ALMOST never fetch lazy. What’s your usecase? Are you aware of DDD aggregates term? Where are your transaction boundaries? Lazy may be a sign that your domain model is not decomposed well.
j
I’m sorry but I disagree. Let’s say you have an author/book/publisher model. I want to find an author. If author 1..* book and author * ..* publisher are eager, they will always be fetched. I believe the opposite is true, you always want to fetch lazy. Maybe only not when you have a 1-1 relationship. That being said, I am not aware of DDD aggregates or have transaction boundaries. So I am intrigued by your comment, since you clearly know more about this than I do!
j
Agree with Jilles, lazy by default is better
j
You almost never want things to be fetched eagerly by default or you’ll end up with the n+1 query problem