java – Hibernate. How does initialize () work
I know that we use lazily to load objects / collections outside the session. We do hibernate Initialize (object obj) so that the object passed as a parameter to the initialize () method is initialized and can be used outside the session scope
But I can't understand how this works I mean, if we're doing it, we'll be eager to extract it in the end, so why are we lazy in configuration and eager to get it at run time
In other words, I want to know how to use hibernate The difference between initialize () and eagerly loading the object
Am I wrong or am I missing something?
Solution
The difference lies in the scope of application
The reason for lazy Collection Association is to avoid loading the collection every time the parent object is loaded, if you don't need it
If you are lazily loading the collection, but for a specific purpose, you need to ensure that the collection is loaded before the session is closed, you can use hibernate Initialize (object obj), as you specified
If you always need to load this collection, you should really load it In most software, this is not the case