Three delayed loading methods of Hibernate

Source: Internet
Author: User
Persistent state: the object establishes a correspondence with the database record and maintains synchronization. The object is bound to the persistent context. In the future, any State Changes and data changes of the object will be under the management of the work unit. This is a persistent state. The default delayed loading method provided by session. Load in hibernate3.2, I think the load is a proxy, or a persistent state. The delayed loading mechanism is proposed to avoid unnecessary performance overhead. The so-called delayed loading means that data loading is performed only when data is actually needed. When using delayed loading, the object used is a proxy object, which contains all the member variables and methods of the proxy object, only in this object, the value of this member variable is null. In hibernate, delayed loading of object objects and delayed loading of sets are provided, and delayed loading of attributes is also provided in hibernate3. Next we will introduce the details of these types of delayed loading.A. Delayed loading of object:To use delayed loading for object objects, you must configure the ing configuration file of the object as follows: 
Whether the values get attribute is null. If it is not null, The getname method of the target object is called. If it is null, a database query is initiated to generate an SQL statement similar to this: select * from user where id+'1' then to query data, construct the target object, and assign the value to the cglib?calback_0.tar get attribute. In this way, through an intermediate proxy object, Hibernate achieves object loading delay. Only when a user initiates an action to obtain object attributes can the user initiate a database query operation. Therefore, the delayed loading of objects is completed through the intermediate proxy class, so only session is available. the load () method uses the object to delay loading, because only the session. the load () method returns the proxy class Object of the object class.B. Delayed loading of Collection types:In the delayed Loading Mechanism of hibernate, it is of the most significant significance for the application of the set type, because it may greatly improve the performance, so hibernate has made a lot of efforts, this includes the independent implementation of JDK collection. In one-to-Multiple Association, the Set set defined to hold the associated object is not Java. util. set type or its subtype, while net. SF. hibernate. collection. set type. hibernate implements delayed loading of set types by using custom collection classes. To use delayed loading for the collection type, we must configure the related section of our object class as follows: <Hibernate-mapping><Class name = "com. Neusoft. entity. User" table = "user"> ..... <Set name = "addresses" table = "Address" lazy = "true" inverse = "true"> <Cache Usage = "read-only"/> <key column = "user_id "/> <one-to-learn class =" com. neusoft. entity. arrderss "/> </set> </class> C. Delayed attribute loading:In hibernate3, a new feature-delayed loading of attributes is introduced, which provides a powerful tool for obtaining high-performance queries. As mentioned above, when reading a big data object, there is a resume field in the user object, which is a Java. SQL. the clob type contains the user's resume information. When we load this object, we have to load this field every time, whether or not we really need it, in addition, reading such big data objects will bring about great performance overhead. In hibernate2, we only break down the user class through the granularity subdivision of the surface performance we mentioned earlier (refer to the discussion in that section ), however, in hibernate3, we can use the property delay loading mechanism to obtain the ability to read data from this field only when we really need to operate on this field, therefore, we must configure our object class as follows: 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.