Could not initialize proxy-no session solution _struts

Source: Internet
Author: User
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>

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.