Hibernate delayed loading profiling and proxy Mode Application

Source: Internet
Author: User

The delayed loading of Hibernae is a very common technique. By default, the set attributes of an object are delayed loading, and the objects associated with the object are also delayed loading by default. Hibernate uses this delayed loading method to reduce the system memory overhead and ensure the Running Performance of Hibernate.

Next we will analyze the "secret" of Hibernate delayed loading ".

Delayed loading of set attributes

When Hibernate initializes a persistent object from the database, does the set attribute of the object be initialized along with the persistence class? If a set attribute contains 100,000 or even millions of records, the collection attribute is crawled while the Persistent Object is initialized, resulting in a sharp reduction in performance. It is entirely possible that the system only needs to use part of the records in the persistence class set attribute, instead of all the set attributes. In this way, it is not necessary to load all the set attributes at a time.

For set attributes, we recommend that you use a delayed loading policy. Delayed loading means that the associated data is loaded from the database only when the system needs to use the set attribute.

For example, the following Person class holds a set attribute. The element type in the set attribute is Address. The code snippet of this Person class is as follows:


Listing 1. Person. java

public class Person
{
// Identify attributes
private Integer id;
// Name attribute of Person
private String name;
// Retain the age attribute of Person
private int age;
// Use Set to save Set attributes
private Set<Address> addresses = new HashSet<Address>();
// The setter and getter methods for each attribute are omitted below
...
}

 

To enable Hibernate to manage the set attributes of the persistence class, the program provides the following ing files for the persistence class:


Listing 2. Person. hbm. xml

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<! -- Map the Person persistence class -->
<class name="Person" table="person_inf">
<! -- Ing id property id -->
<id name="id" column="person_id">
<! -- Define the primary key generator policy -->
<generator class="identity"/>
</Id> <! -- Used to map common attributes -->
<property name="name" type="string"/>
<property name="age" type="int"/>
<! -- Ing set Attributes -->
<set name="addresses" table="person_address" lazy="true">
<! -- Specify the associated foreign key column -->
<key column="person_id"/>
<composite-element class="Address">
<! -- Map common attributes detail -->
<property name="detail"/>
<! -- Map common attributes zip -->
<property name="zip"/>
</composite-element>
</set>
</class>

 

From the code of the above ing file, we can see that the Address class in the set attribute of Person is just a common POJO. The Address class contains the detail and zip attributes. Because the Address class code is very simple, the code of this class is not given.

<Set... /> the code in the element specifies lazy = "true" (for <set... /> for the element, lazy = "true" �� default value), which specifies that Hibernate will delay loading the Address object in the Set property.

For example, use the following code to load the Person object with ID 1:

 Session session = sf.getCurrentSession();
Transaction tx = session.beginTransaction();
Person p = (Person) session.get(Person.class, 1);
//<1> System.out.println(p.getName());

 

The above Code only needs to access the Person object with ID 1 and does not want to access the Address object associated with this Person object. There are two situations:

  • If loading is not delayed, Hibernate immediately captures the associated Address object when loading the data records corresponding to the Person object.
  • If delayed loading is used, Hibernate only loads the data records corresponding to the Person object.

Obviously, the second approach can reduce the interaction with the database and avoid the memory overhead caused by loading the Address entity-this is also the reason why Hibernate enables delayed loading by default.

The question now is, how is delayed loading implemented? What is the addresses attribute value of the Person object when Hibernate loads the Person object?

To solve this problem, we<1>Set a breakpoint in the Code and Debug it in Eclipse. Now we can see the output shown in 1 in the Eclipse Console window:


Figure 1. Console output of delayed loading set attribute

As shown in the positive 1 output, Hibernate only captures data from the data table corresponding to the Person object and does not capture data from the data table corresponding to the Address object. This is delayed loading.

So what is the addresses attribute of the Person object? In this case, the result 2 is displayed in the Variables window of Eclipse:


Figure 2. Set attribute values for delayed Loading

As shown in the box in Figure 2, this addresses attribute is not a familiar implementation class such as HashSet and TreeSet, but a PersistentSet implementation class, this is an implementation class provided by Hibernate for the Set interface.

The PersistentSet object does not really capture the data of the underlying data table, so it naturally Cannot initialize the Address object in the set. However, the PersistentSet set contains a session attribute, which is a Hibernate session. When the program needs to access the PersistentSet set element, persistentSet uses this session attribute to capture data records corresponding to the actual Address object.

  • 1
  • 2
  • 3
  • Next Page

Related Article

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.