Issue
So I have a relationship between two objects Say product and orders, this being 1 to many
i understand this is set to lazy by default.
my question is in spring boot, if I have no transactions as im only reading. Can I call getProducts(), and then pass this object to another service and then call getOrder(). Or will the session be closed. Basically I dont want to load all the data
Solution
No you can't call getOrder() outside the transaction boundary, it will throw an exception. Two possible ways.
Initialize all you need within transaction boundary by simply calling .size() method on your One To Many collections and use them at later stages. This way you'll make sure your Transaction Boundary is limited and you're not keeping up the Connection for long.
If you dont want Transaction at all, write a separate repository method to fetch Collections using Fetch join.
Answered By - Ashutosh Srivastav