Home » Java » The method of lazy loading in Hibernate

The method of lazy loading in Hibernate

method for solving the lazy loading in Hibernate

First:


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

Second:


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)


Third:

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:


public

Department
getDepartmentWithChildren (Integer ID {
)
Department = s

Department
Dao.getEntity (ID);

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

return s;

}





Latest