Solving the problem of delay loading in SSH integration session

Source: Internet
Author: User

Problem Description: When using Hibernate and struts, you will often encounter the following bugs:

org.apache.struts2.json.JSONException: org.apache.struts2
failed to lazily initialize a collection
of role: blank.domain.Route.stopRoutes, could not initialize proxy - no Session

Solution:

Method One, add the configuration of the session filter in Web. xml

<!--delayed closing sessionThe order is above the STRUTS2 filterOtherwise, delaying the closing session does not work--
<filter>
<filter-name>opensession</filter-name>
<filter-class>
Org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>opensession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Because org.springframework.orm.hibernate4.support.OpenSessionInViewFilter specifies the value of Sessionfactorybeanname in this class is " Sessionfactory "

Therefore, in the spring configuration file, the configuration associated with sessionfactory needs to change the name to "Sessionfactory", otherwise it will not be reported to find a bug sessionfactory.

Specific as follows:

<!--sessionfactory Factory--
<bean id= "sessionfactory"
class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name= "Configlocation" >
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>

<!--Configure hibernatetemplate templates--
<bean id= "Hibernatetemplate" class= "Org.springframework.orm.hibernate4.HibernateTemplate" >
<property name= "Sessionfactory" ref= "sessionfactory"/>
</bean>

<!--configuration Hibernatedaosupport--
<bean id= "Hibernatedaosupport"
Class= "Org.springframework.orm.hibernate4.support.HibernateDaoSupport"
Abstract= "true" >
<property name= "Hibernatetemplate" ref= "Hibernatetemplate"/>
</bean>

<!--hinernate transaction Manager--
<bean id= "Hibernatetransactionmanager"
class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "sessionfactory"/>
</bean>

Method Two, set the Lazy property to False (not recommended) in Hibernate's *.hbm.xml configuration file.

Case:

In StopRoute.hbm.xml

<many-to-one name= "Route" class= "Route" cascade= "save-update" fetch= "join" lazy= "false" >
<column name= "rid"/>
</many-to-one>
<many-to-one name= "Stop" class= "Stop" cascade= "save-update" fetch= "join" lazy= "false" >
<column name= "Sid"/>
</many-to-one>

In Route.hbm.xml

<set name= "Stoproutes" inverse= "true" cascade= "delete" lazy= "false" >
<key>
<column name= "rid"/>
</key>
<one-to-many class= "Stoproute"/>
</set>

Solving the problem of delay loading in SSH integration session

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.