Lazy load ERROR org.hibernate.lazyinitializationexception:42-could not initialize proxy-...

Source: Internet
Author: User

No session problem, that is, lazy loading

The problem with lazy loading is that when we finish invoking a method in the action, we find that the session opened in DAO is closed when we want to display the information we want on the JSP page.

For example, the first arrow indicates that we return through the front page action,action will call DAO through the service layer to access the database, when the value is removed from the database and returned to the action, and then back to the foreground page. We know that the session will only open when we invoke a class getter () method, but unfortunately, the sessionfactory injected in Hibernatetemplate has been closed before the action returns to the foreground page. This is when the front page call getter () method, want to open the session, but Sessionfactory has been closed, so the background will be error, show no session. It means that I can't find the session.

This time can be resolved in the following ways:

1, the Pojo class of the fetch of the lazy to change to eager, this time if you are involved in the operation of a number of foreign key relations, it is necessary to this a few foreign key relations of the fetch lazy to change to eager. Here lazy is the meaning of lazy loading, that is, when querying the information of a table, the information of the associated table will not be found out. If you change to eager, it means that the information about the associated tables is also loaded. Obviously, this kind of operation will load something we do not need at all, or we may just need to load the information of a table, but automatically load the associated table. So this kind of operation performance is very poor, is not recommended to use.

2, the configuration filter opensessioninviewfilter, as the name implies, this filter refers to the page to open the session of the service, that is, delay the session open state, in the foreground page after calling the method and then close the session, configured as follows:

<filter>

<filter-name>OpenSessionInView</filter-name>

<filter-class>

Org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

</filter-class>

</filter>

<filter-mapping>

<filter-name>OpenSessionInView</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

Note that this filter is to be configured to the front of the struts filter, otherwise it will not work, because the configuration at the back of the struts filter, the data has been received, that is, Sessionfactory has been closed, This time you come to configure the session to extend to the view, has no effect, so to configure the opensessioninviewfilter in front of struts. However, it is important to note that the above configuration is not a complete configuration, the complete configuration should be in the filter configuration of the following information:

<filter>

<filter-name>OpenSessionInView</filter-name>

<filter-class>

Org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

</filter-class>

<init-param>

<param-name>singleSession</param-name>

<param-value>true</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>OpenSessionInView</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

The above configuration means that the session is configured as a single case. However, if the above configuration, in the subsequent session of the operation will be problematic, and if not configured as a singleton, the performance is very low. So this method is not recommended.

3. Manual operation (recommended)

A) Action in action

Actions in action refer to passing objects to the foreground when we pass the object to the foreground page (mainly JSP). For example, in action we need to pass the question object to the foreground to show, but also need to find out the type of the problem belongs to, this time can be in the page action in addition to passing question object, The type object is then passed to the page, based on the other properties of the question, such as Tid, to find the corresponding type. But the trouble is that in addition to their own in the DAO to add the method, if a page management of several data tables, the transfer of object properties is relatively more.

b) operation in Daoimpl (strongly recommended)

The manual setting in Daoimpl is the most suitable way for me, and it is also a kind of more respected. In this way, in the Daoimpl operation, it is necessary to use the Hibernatecallback inner class method. That is, call the Super.gethibernatetemplate (). Execute () method, write the inner class, after getting a list collection, take the object of the collection, call the Getter () method of the associated class to be used, you can open the session, Typically, you do not have to invoke the Getter () method of the primary key.

Example:

Public Question FindByID (final Integer id) throws Exception {

Return Super.gethibernatetemplate (). Execute (

New Hibernatecallback<question> () {

@SuppressWarnings ("Unchecked")

Public Question Doinhibernate (session session)

Throws Hibernateexception, SQLException {//with Hibernatecallback

String hql = "from Question as Q WHERE q.qid=?"; Building HQL Statements

Query query = session.createquery (HQL); Create Query

Query.setinteger (0, id); Set placeholder

list<question> all = Query.list (); Get a specific list set with only one question object stored in it

if (all.size () > 0) {//Collection not empty

Question vo = all.get (0); Remove Question Object

Vo.gettype (). GetTitle ();

// Call Type the GetTitle method, this will put the type related Session open, in JSP The data can also be called when called

vo.getanswers (). Size ();

// call Getanswers () method to put the session Open, eliminate lazy loading

                                                                     if (vo.getanswer () = null) {//   If there is a best answer

Vo.getanswer (). getcontent (); Eliminate lazy loading

}

return VO; Return VO

}

return null;

}

});

}

Lazy load ERROR org.hibernate.lazyinitializationexception:42-could not initialize proxy-...

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.