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