Could not initialize proxy-no session error occurrence
When using STRUTS+HIBERNATE+SPRINGMVC, you use the standard query criteria to make a cascading query that throws this error
Student entity class public class
Student {
private Integer ID;
private String name;
private String password;
Class object and students a one-to-many relationship
private Classes Classes = new Classes ();
Course Collection and student many-to-many relationship
private set<course> courses = new hashset<course> ();
}
DAO Layer Method: The standard query automatically detects the student's class and the selected course public
list<student> GetList () {return
getcurrentsession (). Createcriteria (student.class). List ();
Reason analysis (personal understanding) Spring's configuration file is configured to automate management transactions so when you query the data in the Student table, Hibernate automatically queries the data for the other two tables associated (class and course information) queries are performed by the session object. But after the student table is checked, spring's transaction management automatically closes the session. As a result, the latter two operations cannot be completed and session is missing. So newspaper exception could not initialize Proxy-no session (Unable to initialize proxy object-no session)
<property name= "Hibernateproperties" > <props> <prop key= "Hibernate.dialect" >org.hibernate.di Alect. mysql5innodbdialect</prop> <prop key= "Hibernate.show_sql" >true</prop> <prop key= "Hibe Rnate.hbm2ddl.auto >update</prop> <!--Configure thread-bound session factory.opensession: Open transaction/operation/COMMIT TRANSACTION/close SES Sion getcurrentsession: Get a thread-bound session, do not need to open/submit data on their own, after the thread end, automatically shut down sessions--> <!--currentsession phase When the previous session of the latter template object, the role is to manipulate the data in the database, the basic is to delete the check--> <prop key= "Hibernate.current_session_context_class" >org. Springframework.orm.hibernate4.springsessioncontext</prop> </props> </property> <!--configuration Transactions-- > <bean id= "Txmanager" class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" > < Property Name= "Sessionfactory" ref= "sessionfactory" ></property> </bean> <!--use spring annotations to open a transaction-- > <tx:annotation-driven transaction-manager= "Txmanager"/>
Business Process layer Annotations Add Spring Auto Management transaction
@Transactional
@Service ("Xkservice") public
class Xkservice {
omit specific
}
Solutions
To configure a filter to open a transaction in Web.xml
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class> Org.springframework.orm.hibernate4.support.opensessioninviewfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern >/*</url-pattern>
</filter-mapping>