I am building out a Spring Boot backend using Hibernate and JPA. I have a user object that I plan to fetch given a JWT per request. But past that I am unsure what would be the best way to fetch related info to the user such as a list of other objects that belong to the user.
The two ways I can see are:
-
Add the list of objects to the user object as a field and lazy load it when needed
- This I can see making the user information object quite complex and perhaps messy if there are multiple of such field
-
Fetch the list of related objects using a repository method given the user id
- I feel like this is cleaner and easier to implement, but doesn’t feel like I am using the ORMness of these frameworks
I think performance would be relatively the same since there would be two queries made each time.
Can someone shed some more light on what approach may be better (or if there are other approaches too)? And in what scenarios should option 1 be implemented vs option 2?
After writing out the user info object using option 1 from above, I felt like the object was complex and hard to read. But then I am wary that I am not using these frameworks properly and maybe should opt for something else.