java - Hibernate: Eager and Lazy loading in case of session.load vs session.get -


i under impression session.load() loads proxy object in cache while session.get() hits database confused after watching javabrains video.

according video when calling below method loading proxy object of userdetails in memory.

user = (userdetails) session.get(userdetails.class, 1);  

structure of userdetails is

enter image description here

while in comment section, guy commented:

there no proxy of user class, instead proxy object of collection created.

now there 2 questions here.

1st: related fetching strategies , creation of proxy objects in case of session.load() , session.get() has been answered below me.

2nd: in case, proxy object create userdetails or collection (still answered).

thanks

here, userdetails parent , address child. hibernate lazy-loading child address. children elements (address in case) not pre-loaded while load parent (userdetails in case).

so, when this:

user = (userdetails) session.get(userdetails.class, 1);  

hibernate not loading child (collection<address>). instead hibernate load address when explicitly access them. hibernate wont hit db address table unless need them , that's purpose of lazy loading.

what means lazy loading while proxy object of userdetails, doesnt hit address table unless try access collection elements explicitly. in other words, need iterate on collection hibernate fetch address table.

you end situation hit database everytime each child (address). so, make explicit call listofaddresses.size() load children @ time.

also note lazy loading happen default one-to-many , many-to-many cases.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -