Home » Java » The method of lazy loading in Hibernate

The method of lazy loading in Hibernate

method for solving the lazy loading in Hibernate


1. in the mapping files need to disable the lazy loading is shown in the join lazy = "false"

this method greatly reduces the efficiency of the program, if a small amount of

can still access


2. configuration in web.xml

> >openSessionInViewFilter >org.springframework.orm.hibernate3.support.OpenSessionInViewFilter > > >openSessionInViewFilter >/*

This is a

solve the lazy loading problem. Using spring openSessionInViewFilter.

openSessionInViewFilter, modified filter in view rendering always open session, solve the lazy loading problem of hibernate,

of the filter must be configured in the Struts2 filter before the increase of performance will be reduced if the access is not recommended (performance)


3. at the service level is forced to initialize the proxy object.

is in the object after the forced to load object attributes (recommended)

For example:


getDepartmentWithChildren (Integer ID {
Department = s

Dao.getEntity (ID);

// and pages questions set
initialization force
for (Student stu): s.getStudents ({
stu.getClasses.size (

return s;