Hibernate: session.load vs session.get

I got the impression that session.load() loads the proxy object into the cache, and session.get() always gets into the database, but I'm confused after watching JavaBrains video .

According to this video, when we call the get method below, it loads the UserDetails proxy object into memory.

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

UserDetails structure is

enter image description here

In the comments section, the guy commented:

there is no proxy of the User class, instead a proxy object is created a collection.

Now there are two questions.

1st: related to fetching strategies and creating proxy objects in the case of session.load () and session.get (), which I answered below.

2nd: In this case, the proxy object will create for UserDetails or for the collection (still need to be answered).

thanks

+5
source share
3 answers

1.Photographic strategies: The effect of selecting strategies when running session.get or session.load ( https://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch20.html#performance-fetching- lazy ).

2. Session.get: It never returns a proxy , According to sleep mode operations: ( https://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/Session.html#get(java. lang.Class , java.io.Serializable))

Returns a constant instance of this entity class with the given identifier or null if there is no such constant instance. (If the instance is already associated with the session, return this instance. This method never returns an uninitialized instance.)

The get tool first checks the cache if there is a fully initialize object present, if so returns this object otherwise hits the database to get the object and returns it after saving in cache space.

3. Session.load: According to the dormant documents:

Returns a persistent instance of this entity class with the given identifier, assuming that the instance exists. This method can return a proxy instance that is initialized on request when a non-identification method accesses it.

The load tool first checks the cache if a fully initialize object present there, and if so returns this object, it returns a proxy (a proxy is a class that delegates to another object. Initially, when it is not initialized, it contains only the primary key. When you call the method, as Javadoc says, it initializes by loading the actual object from the database and delegates into this loaded object) of this object, "assuming that this instance exists."

Note. It is important to note that the load never throw an exception method. You will get an ObjectNotFoundException if you try to retrieve another property instead of the primary key from the proxy object. How does it get into the database to load an object that does not exist there.

+3
source

Here UserDetails is the parent and Address is the child. Sleep mode is actually lazy - it loads the child's Address . Therefore, in the end, all child elements ( Address in this case) are not preloaded when you load the parent ( UserDetails in this case).

So when you do this:

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

Hibernation does not actually load all child elements ( Collection<Address> ). Instead of Hibernate, load Address only with direct access to them. This way hibernate will not use DB for the Address table unless you really need them and that is the purpose of lazy loading .

What Lazy loading means is that as long as you get the UserDetails proxy object, it really doesn't get into the Address table unless you try to access the elements of the collection explicitly. In other words, you need to iterate over the sleep collection to get the Address table.

You may find yourself in a situation where each time you get into the database for each child ( Address ). So make an explicit call to listOfAddresses.size() to load all the children at a time.

Also note that Lazy loading will occur by default for One-to-Many and Many-to-Many cases.

+1
source

2 Ques Ans: The proxy will be the Address for your question context, you can say that. In depth or in detail, the first sleep mode will create a proxy for userdetail, but as soon as the request gets into this proxy server, there will be data. But since hibernation by default is lazy loading, and Address is a child of UserDetail, it will only be returned as a proxy, and you will only get the id value for the address (primary key value). Do not confuse with a proxy server. A proxy will be created for all classes of either the parent class (UserDetail) or the child class (Address), but for the child class it will have only id data.

-1
source

Source: https://habr.com/ru/post/1271677/


All Articles