In Spring Boot (Part 2) Lesson while adding a product item to a cart, Mosh uses the Cart Entity’s cartItems getter method to filter the list of products by the productId. However, there is @OneToMany relationship in the cartItems field of the Cart Entity. This makes the cartItems field lazy loaded by default. So, when we use the getters to get the cartItems, we should actually get the LazyInitializationException, but for some reason I’m not getting that exception.
In Sprint Boot Part 1, Mosh mentioned that all the repository methods are transactional and this led me to think that when checking for the cart and product entities if they exist by their id, the cart and product entity will be in detatched mode unless whole method is marked as @Transactional. But after some searching in ChatGPT, I found out that the Spring Boot inherently has an implementation of this OSIV (Open Session In View) pattern where the @Transactional life cycle extends to the HTTP Request. Due to this, we don’t need to specify the methods to be transactional and the lazy loaded fields work by default.
I just wanted to know if I have a correct understanding of this concept. Can anyone confirm it?