I'm new on object persistence with Hibernate. I'm reading a book to try to understand what's the difference between lazy set to false, eager and immediate fetching but I don't see any difference. Any help??
Thanks in advance!
I'm new on object persistence with Hibernate. I'm reading a book to try to understand what's the difference between lazy set to false, eager and immediate fetching but I don't see any difference. Any help??
Thanks in advance!
Main difference between immediate and eager fetching is with immediate fetching, there are two separate queries fired one for the owning object and other for the associated object. But for eager fetching, there will be only one query fired including inner join/outer join to get associated object with the entity.
I believe "immediate" is a synonym for "eager" (Eager being the JPA, which Hibernate implements, definition)
Lazy is as it sounds. Don't do anything until you have to. Eager means ... as it sounds.
If Foo has a Collection, and you set it to lazy, then only when you need the contents fo that collection are the selected, loaded, etc. whereas if it is eager, it will load the Bars at the time it loads Foo. This can be problematic if you eagerly load a collection of entities that eagerly load a collection of entities, and so on.
However, if you make everything lazy, then you may suffer from excess queries and round trips. You have to choose what is right for how you will be using the entity.
Start with lazy if you want a one-line rule of thumb..