To deal with this lazy loading problem, the final approach is to use a support class for Hibernate provided by Spring. Its main meaning is to open the Hibernate Session when a page request is initiated, and maintain this Session so that Hibernate's Session The life cycle becomes longer until the request ends, which is implemented through a Filter. So, if we now want to use Hibernate's lazy loading feature, and also want to extend the life cycle of the session and display the data on the page (through the action layer), then we have to add the following configuration to the web.xml file:
OpenSessionInViewFilter org.springframework.orm.hibernate3.support.OpenSessionInViewFilter OpenSessionInViewFilter *.action
Note: 1) OpenSessionInViewFilter is the filter name, *.action means intercepting all actions, or /*
2) Non-web page request ( Such as scheduled tasks) can be processed in the following way (the Hibernate.initialize(Object proxy) method forces loading, which is equivalent to dynamically changing to lazy=fals)
/** * @Author masl - 2017/9/28 14:22 * @param setRepaymentId * @param initSubs :是否初始化关联表数据 * @return */ @Override public SetRepayment findSetRepaymentById(Integer setRepaymentId, boolean initSubs) { SetRepayment setRepayment = null; if (setRepaymentId != null) { setRepayment = setRepaymentDao.get(setRepaymentId); if (setRepayment != null && initSubs) { Hibernate.initialize(setRepayment.getSetIncomes()); } return setRepayment; } return null; }
The above is the detailed content of Examples of handling failed lazy loading of fetch=FetchType.LAZY in hibernate. For more information, please follow other related articles on the PHP Chinese website!