and then would it be possible to load them (option...
# spring
d
and then would it be possible to load them (optionnaly) within the controller ?
t
Are you sure posts aren't loaded eagerly? When they are accessed and a transaction is available, they would be loaded automatically in the background.
k
it doesn't matter (that much) if eagerly or lazy, since jackson will call "getter" during serialization and it will be loaded. check spring data projections or json views
j
Spring comes with something called open transaction in view , that might obscure some of the lazy vs eager effects. Most people think it should be turned off but no one seems to have a simple answer for the alternative. See my favorite github issue discussion ever https://github.com/spring-projects/spring-boot/issues/7107
k
uf what a discussion
d
hey thanks guys
ya I’m trying to understand…
all I can say at this point is that even though I’m calling a list of all users (without their posts), I can see in the log, a query for each user to get their posts. basically a n+1
I should be able to set it up so that I can load the post optionaly from the controller.
j
Hibernate does not load the posts eagerly. But since you're returning users from the controller, and since users have posts, Jackson serializes them by iterating through the posts, which causes the posts to be loaded, lazily. If you map the users to user DTOs which don't have posts, and return those DTOs, the posts won't be loaded, because no one will iterate through the posts.
👍 1
d
thanks @jbnizet I’ll try it but it looks like the solution. (I’m learning Spring and Kotlin, so I have to undersant what DTO is…) Thanks again
j
Data Transfer Object. It just means a different pojo with all the same fields as your jpa entity but its not a jpa entity. Only the fields you explicitly populate will have data. So if you don't want the list of posts just don't copy the posts from your JPA entity into your dto.
d
I see, it’ like a layer/filter between a model(s) and the response, right ?
but then where do people typically put these ? In a
DTOs
folder at the same level than my
models
folder ?
j
I don't think there's a right and wrong answer to this question. Sometimes you might want your dtos in a completely different module so that you can publish a jar artifact for them and share them with your REST consumers
j
I’d add that even though DTOs and entities look similar in toy demo projects, in real-life projects they are often quite different: entities represent how you store data in the database, whereas DTOs represent what you send and what you receive from the client (often a web layer). And quite often, those are very different things that shouldn’t be coupled to each other (and should thus not use the same classes). They don’t belong to the same layer, and, IMHO, shouldn’t be designed the same way. The most common example being the one you posted: even though a user has posts (and maybe also an address, which has a city, which has a country, which has provinces, which have administrators, etc. etc.), you don’t want all of that being sent to the client. In the same vein, the client, when creating a post, will probably just send the ID of the receiver of the post for example, and not a complete user object.
👍 1
d
exactly.
Else in terms of performance, my app will be terrible
k
also check cqrs
d
It would be nice if I could optionnaly load the relationship with something like
user.with('posts')
. Like they have in Vapor (Swift) https://docs.vapor.codes/4.0/fluent/relations/#eager-loading, rails or Adonis.js. Else I could create a method
with()
in each model but that should be enshrined in JPA or hibernate. I’ll think about…