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. Session. load: the default delay loader provided by hibernate3.2... SyntaxHighlighter. all (); persistent state: the object establishes a ing relationship with the database records 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 object ing configuration file as follows:
......
You can enable the delayed loading feature of an object by setting the lazy attribute of the class to true. If we run the following code:
User user = (User) session. load (User. class, "1"); (1)
System. out. println (user. getName (); (2)
When running at (1), Hibernate does not initiate data query. if we use some debugging tools (such as JBuilder2005's Debug tool) or debug breakpoint monitoring, at this time, we will be surprised to find that the returned user object may be a User $ EnhancerByCGLIB $ bede8986 type object, and its attribute is null. The session. load () method returns the proxy class object of the object. the object type returned here is the proxy class object of the User object. In Hibernate, CGLIB is used to dynamically construct the proxy class object of a target object, and the proxy class object contains all the attributes and methods of the target object, all attributes are assigned null values. With the memory View displayed by the container, we can see that the correct userobject is included in the cglib?calback_0.tar get attribute of the agent object,
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 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:
...
By adding The lazy attribute of the element is set to true to enable the delayed loading feature of the collection type. Let's look at the following code:
User user = (User) session. load (User. class, "1 ");
Collection addset = user. getAddresses (); (1)
Iterator it = addset. iterator (); (2)
While (it. hasNext ()){
Address address = (Address) it. next ();
System. out. println (address. getAddress ());
}
When the program is executed at (1), no query is initiated for the associated data to load the associated data. only when the program runs at (2, the real data read operation starts. at this time, Hibernate searches for object objects that meet the condition based on the qualified data index in the cache. Here we introduce a new concept-data index. next we will first take a look at what is data index. In Hibernate, the set type is cached in two parts. First, the IDs of all objects in the set are cached, and then the object IDs are cached, it is the so-called data index. When you look for a data index, if the corresponding data index is not found, a select SQL statement is executed to obtain qualified data and construct a collection of entity objects and data indexes, then return the collection of object objects and include the object objects and data indexes in the cache of Hibernate. On the other hand, if the corresponding data index is found, the id list is retrieved from the data index, and the corresponding entity is searched in the cache based on the id. if it is found, it is returned from the cache, if not, select SQL query is initiated. Here we can see another problem, which may affect the performance. this is a cache policy of the collection type. If we configure the collection type as follows:
.....
Here we have applied Configuration. if this policy is used to configure the collection type, Hibernate will only cache the data index, but will not cache the object in the set. Run the following code for the above configuration:
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 ());
}
System. out. println ("Second query ......");
User user2 = (User) session. load (User. class, "1 ");
Collection it2 = user2.getAddresses ();
While (it2.hasNext ()){
Address address2 = (Address) it2.next ();
System. out. println (address2.getAddress ());
}
Run this code to get output similar to the following:
Select * from user where id = '1 ';
Select * from address where user_id = '1 ';
Tianjin
Dalian
Second query ......
Select * from address where id = '1 ';
Select * from address where id = '2 ';
Tianjin
Dalian
We can see that when the second query was executed, two queries were performed on the address table. why? This is because after loading an object for the first time, according to the configuration of the cache policy of the collection type, only the collection data index is cached, and the object objects in the collection are not cached, therefore, when the object is loaded again for the second time, Hibernate finds the data index of the corresponding object, but it cannot find the corresponding object in the cache according to the data index, therefore, Hibernate initiates two select SQL queries based on the data index. This results in a waste of performance. how can we avoid this situation? We must specify a cache policy for the objects in the collection type. Therefore, we need to configure the collection type as follows:
.....
At this time, Hibernate will cache the objects in the set type. if you run the above code again based on this configuration, the following output will be obtained:
Select * from user where id = '1 ';
Select * from address where user_id = '1 ';
Tianjin
Dalian
Second query ......
Tianjin
Dalian
In this case, no SQL statement is used to query data indexes, because the entity objects stored in the set type can be directly obtained from the cache.
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:
......
Through The lazy attribute of the element is set to true to enable delayed loading of the attribute. in Hibernate3, a Class extender is used to enhance the Class file of the object Class, through enhancement of the booster, the logic of the CGLIB callback mechanism is added to the entity class. here we can see that the delayed loading of the attribute is implemented through CGLIB. CGLIB is an open-source project of Apache. this class library can manipulate the bytecode of java classes and dynamically construct class objects that meet the requirements according to the bytecode. Based on the preceding configuration, run the following code:
String SQL = "from User user where user. name = 'zx '";
Query query = session. createQuery (SQL); (1)
List list = query. list ();
For (int I = 0; I User user = (User) list. get (I );
System. out. println (user. getName ());
System. out. println (user. getResume (); (2)
}
When executed at (1), an SQL statement similar to the following will be generated:
Select id, age, name from user where name = 'zx'; www.2cto.com
At this time, Hibernate will retrieve all the field data corresponding to the non-delayed loading attribute in the User object. when it is executed at (2), it will generate an SQL statement similar to the following:
Select resume from user where id = '1 ';
In this case, a real read operation is initiated on the resume field data.
Author: oh_Mourinho
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