Hibernate lazy Load (lazy load)

Source: Internet
Author: User
Tags advantage

Lazy loading is delayed loading and delay loading. When two or more tables use hibernate to manage cascading relationships, when a table is loaded into the JVM memory, its related tables can be staged in the database cache, and when the related table data needs to be loaded into the JVM memory, the lazy loading mechanism can reduce unnecessary overhead in memory to improve the performance of the program.

Note that when lazy loading is used, if the session is closed, the connection to the database is disconnected, and if you want to access the child table data, because the child table data is stored in the database cache, and the connection is closed, The access to the child table data throws a Lazyinitializationexception exception.

Workaround:

1) Do not use lazy loading: Change the Lazy property in the child table configuration file to False and do not use lazy loading for a cascade relationship

<many-to-one name= "Parent" class= "Parrentclass" column= "ParentID" lazy= "false" ></many-to-one>

Note: also applicable for Many-to-many

Or you can set the entire child table to not use lazy loading for any parent table, then lazy loading is not used for the entire child table

<class name= "ChildClass" table= "ChildTable" lazy= "false" >

2) Use the filter to postpone the close session until the page results are obtained. This can be achieved using spring opensessioninviewfilter (which is a filter).

Adding a configuration to Web. xml

<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

3) Manually load the required child table data from the database cache into the JVM memory. For example, classroom has a one-to-many relationship with student entity classes (many-to-many also apply), i.e.

Public Classroom {

Private set<student> students;

.........

}

The DAO layer has

List<classroom> list = Session Get result set operation

for (classroom classroom:list) {
Hibernate.initialize (Classroom.getstudents ()); Loops patch the Student collection corresponding to the classroom entity into the JVM memory

}

Session.close ();


Method Comparison: The first method is simple and rough, the advantage is not too concerned about delay loading exception, because it will not occur, the disadvantage is that each time all relevant data loaded into the JVM memory, consumption of memory resources; The second method is a common method in Javaweb, with the advantage of reasonably determining the load time and loading as needed. The disadvantage is that it can only be used in the Java EE, but not for the J2SE project; The third method, the advantage is the flexibility to control when loading, the disadvantage is to add extra code, and need to load the child table data but forget to pull it into the JVM memory before the session closes. Will throw Lazyinitializationexception


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.