Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the delayed loading technology of Hibernate

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is Hibernate deferred loading technology". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is Hibernate deferred loading technology".

Objects that are deferred loaded by Hibernate:

◆ hibernate 2 for entity objects and collections

◆ hibernate 3 also provides delayed loading of properties.

Among them, the delayed loading characteristic of the collection is the most significant.

Hibernate lazy loading of solid objects:

The class specified in the hibernate configuration file

Delayed loading of Hibernate for collection types:

Specify lazy=true in set

This only loads the actual dataset from the database through session when the collection object associated with the object is actually loaded.

The Hibernate.initialize method forces Hibernate to load the associated set of objects immediately, for example:

Hibernate.initialize (user.getAddress ())

Cache for collection types:

If caching is set for a collection class, such as

When Hibernate caches collection types, it is saved in two parts. The first is the id list of all entities in the collection, followed by the individual entity objects.

The cache usage= "read-only" here only causes Hibernate to cache the data index. That is, only the data indexes in the collection are cached, and the individual entity elements in the collection are not included.

Entities in the collection are cached only if you specify cache usage= "read-write".

Hibernate delayed loading of the property:

Declare lazy=true in the property node, and you also need to enhance the binary Class file of the POJO class with the help of the Hibernate class enhancer.

Collection in hibernate

Hibernate's independent implementation of the JDK Collention interface:

Because the traditional Java Set, Map and List implementations can not meet the requirements, Hibernate provides its own implementation according to these interfaces.

Implementation of Hibernate:

◆ unordered sets: Set, Bag, Map

◆ ordered set: List

Bag is the equivalent of a Set that allows repeating elements to exist. Because Hibernate is its own Collection implementation, the following statement makes an error, Set hset = (HashSet) user.getAddresses (); reports a java.lang.ClassCastException at run time because it actually returns an object of type org.hibernate.collention.Set.

All the reasons why we have to declare Collective-type properties with JDK Collection Interface (such as Set, Map) rather than specific JDK Collection implementation classes (such as HashSet, HashMap) when writing POJO. For example, it should be the saving process of private Set addresses; rather than private HashSet addresses;collection type properties.

For example

Public class TUser implements Serializable {private Set addresses = new HashSet ();. }

After you create an instance of TUser, you can add the associated address object to it:

TUser user = new TUser (); TAddress addr = new TAddress (); addr.setAddress ("HongKong"); user.getAddress () .add (addr); session.save (user)

The user object has changed after being processed by Hibernate. First, due to the insert operation, the id value is generated and populated into the id property of the user object. On the other hand, Hibernate uses its own collection implementation to replace the HashSettype addresses property in user and populates it with data.

Thank you for your reading, the above is the content of "what is Hibernate delayed loading technology". After the study of this article, I believe you have a deeper understanding of what Hibernate delayed loading technology is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report