Get the App
SLTechnology News&Howtos  ›  Development  › 

Introduction to the delayed loading feature of Hibernate collection types

Shulou Source: shulou.com Published: 2022-06-03 11:41:53 11月15日 Update

This article focuses on "introduction to the delayed loading features of Hibernate collection types". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "introduction to the delayed loading features of Hibernate collection types".

Delayed loading of Hibernate collection types

In the delayed loading mechanism of Hibernate, the application of collection type is the most significant, because it is possible to greatly improve the performance. Hibernate has made a lot of efforts, including the independent implementation of JDK Collection. In one-to-many association, the Set collection that we define to hold associated objects is not the java.util.Set type or its subtype, but the net.sf.hibernate.collection.Set type. By using the implementation of custom collection classes, Hibernate implements delayed loading of collection types. In order to use deferred loading for the Hibernate collection type, we must configure the association section of our entity class as follows:

...

Turn on the delayed loading feature of the Hibernate collection type by setting the lazy property of the element to true. Let's look at the following code:

User user= (User) session.load (User.class, "1"); Collection addset=user.getAddresses (); Iterator it=addset.iterator (); while (it.hasNext ()) {Address address= (Address) it.next (); System.out.println (address.getAddress ());}

When the program executes to the second row, it does not initiate a query on the associated data to load the associated data. It is only when the program runs to the third row that the real data read operation begins, and then Hibernate will find qualified entity objects according to the eligible data index in the cache.

At this point, I believe that you have a deeper understanding of the "introduction to the delayed loading features of Hibernate collection types". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Types delays associations properties data content entities objects conditions learning queries utility deeper significant for this code elements interests including only Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information macOS vpn Apple MariaDB