在调试ssh项目的时候总是报错严重: Servlet.service() for servlet action threw exception
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
at com.csms.bean.CustomerQuestion$$EnhancerByCGLIB$$cb37539d.getCreateEmployee(<generated>)
at com.csms.dao.CustomerQuestionDAO.findCreateEmployee(CustomerQuestionDAO.java:62)
at com.csms.service.impl.CustomerQuestionService.findEmployee(CustomerQuestionService.java:34)
at com.csms.action.CustomerServiceAction.goTransQuestion(CustomerServiceAction.java:113)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at com.csms.processor.EncodingProcessor.process(EncodingProcessor.java:17)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source) 
网上说的解决方法是在 hbm文件中的 set 处添加lazy="false"
可是修改过以后还是出现这样的错误 试了好多的方法都不行 
麻烦高手指点一下 小弟谢过 O(∩_∩)O~

解决方案 »

  1.   

    不是设置lazy="false"就能解决的,是你的session关闭了,你要保证session是打开的才行,这和你的具体开发环境有关系。说的详细一点吧。
      

  2.   

    我把代码贴一下好了
    CustomerQuestion.hbm.xml
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping 3.0 DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><hibernate-mapping package="com.csms.bean">
    <class
    name="CustomerQuestion"
    table="customer_question"
    >

    <meta attribute="sync-DAO">true</meta> <id
    name="id"
    type="integer"
    column="id"
    >
    <generator class="increment"/>
    </id> <property
    name="type"
    column="type"
    type="integer"
    not-null="true"
    length="2"
    />
    <property
    name="createDate"
    column="createDate"
    type="date"
    not-null="true"
    length="10"
    />
    <property
    name="resolveDate"
    column="resolveDate"
    type="date"
    not-null="false"
    length="10"
    />
    <property
    name="resolveMethods"
    column="resolveMethods"
    type="string"
    not-null="false"
    />


    <property
    name="resolveState"
    column="resolveState"
    type="integer"
    not-null="true"
    length="1"
    />
    <property
    name="resultState"
    column="resultState"
    type="integer"
    not-null="true"
    length="1"
    />
    <property
    name="title"
    column="title"
    type="string"
    not-null="true"
    length="50"
    />
    <property
    name="description"
    column="description"
    not-null="false"
    />
    <property
    name="customerName"
    column="customerName"
    type="string"
    not-null="false"
    length="20"
    />
    <property
    name="customerContact"
    column="customerContact"
    type="string"
    not-null="false"
    length="50"
    />

    <many-to-one
                 name="createEmployee"
                 column="createEmployee"
                 class="Employee"
                 cascade="save-update"
                 lazy="false" 
            >
            </many-to-one>
    <many-to-one
                 name="transFromEmployee"
                 column="transFromEmployee"
                 class="Employee"
                 cascade="save-update"
                 fetch="select"
            >
            </many-to-one>
            <many-to-one
                 name="transToEmployee"
                 column="transToEmployee"
                 class="Employee"
                 cascade="save-update"
                fetch="select"
            >
            </many-to-one>
            
    </class>
    </hibernate-mapping>Employee.hbm.xml
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping 3.0 DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><hibernate-mapping package="com.csms.bean">
    <class
    name="Employee"
    table="employee"
    > <meta attribute="sync-DAO">true</meta> <id
    name="id"
    type="integer"
    column="id"
    >
    <generator class="increment"/>
    </id> <property
    name="workId"
    column="workId"
    type="string"
    not-null="true"
    length="11"
    />
    <property
    name="name"
    column="name"
    type="string"
    not-null="true"
    length="10"
    />
    <property
    name="department"
    column="department"
    type="string"
    not-null="false"
    length="20"
    />
    <property
    name="type"
    column="type"
    type="integer"
    not-null="true"
    length="2"
    />
            <set 
                name="customerQuestions"
                inverse="true"
                cascade="all"
                outer-join="true"
                lazy="false"
            >
                <key column="transToEmployee"/>
                <one-to-many class="CustomerQuestion" />
            </set>
    </class>
    </hibernate-mapping>
    Action文件
     public ActionForward goTransQuestion(ActionMapping mapping,
                ActionForm form, HttpServletRequest request,
                HttpServletResponse response)  {
            //从表单中获得问题ID
            DynaActionForm dform = (DynaActionForm)form;
            Integer questionId = Integer.valueOf(request.getParameter("id"));
           
            //根据问题ID获得问题对象
            CustomerQuestion question = questionService.findQuestionById(questionId);
            question.getTitle();
            
            //初始化问题的解决状态和关闭状态
            dform.set("resolveState",String.valueOf(question.getResolveState()));
            dform.set("resultState",String.valueOf(question.getResultState()));
           
            //获得所有参与客服管理的人员列表
            List employeeList = new ArrayList();
            employeeList = employeeService.findAllEmployee();
            
            //获得建立问题人员对象   调用此处的时候开始出错的
            String createEmployee = questionService.findEmployee(questionId,0).getName();
            
            //获得传来问题的人员对象
            String transFromEmployee = questionService.findEmployee(questionId,1).getName();
            
            //获得最后解决问题的人员对象
            String transToEmployee = questionService.findEmployee(questionId,2).getName();
            
            //将上述对象存入Request对象中,传递给页面
            request.setAttribute("questionId",questionId);
            request.setAttribute("question",question);
            request.setAttribute("employeeList",employeeList);
            request.setAttribute("createEmployee",createEmployee);
            request.setAttribute("transFromEmployee",transFromEmployee);
            request.setAttribute("transToEmployee",transToEmployee);
            
            //跳转到客服人员转发问题页面
            return mapping.findForward("GoTransQuestion");
        } CustomerQuestionService.java
    package com.csms.service.impl;import java.util.List;import com.csms.bean.CustomerQuestion;
    import com.csms.bean.Employee;
    import com.csms.dao.iface.ICustomerQuestionDAO;
    import com.csms.service.iface.ICustomerQuestionService;public class CustomerQuestionService implements ICustomerQuestionService { private ICustomerQuestionDAO customerQuestionDAO ;

    public void setCustomerQuestionDAO(ICustomerQuestionDAO customerQuestionDAO) {
    this.customerQuestionDAO = customerQuestionDAO;
    } public void closeQuestion(Integer questionId) {
    // TODO Auto-generated method stub
    customerQuestionDAO.closeQuestion(questionId);
    } public void deleteQeustion(CustomerQuestion question) {
    // TODO Auto-generated method stub

    } public List<CustomerQuestion> findAllUnResolvedQuestions() {

    return customerQuestionDAO.findAllUnResolvedQuestions();
    } public Employee findEmployee(Integer questionId, Integer employeeType) {
      if(employeeType==0) return customerQuestionDAO.findCreateEmployee(questionId);
            if(employeeType==1) return customerQuestionDAO.findTransFromEmployee(questionId);
            if(employeeType==2) return customerQuestionDAO.findTransToEmployee(questionId);
            return null;
    }

    public CustomerQuestion findQuestionById(Integer questionId) {
    return customerQuestionDAO.findQuestionById(questionId);
    } public void saveQuestion(CustomerQuestion question) {
    customerQuestionDAO.saveQuestion(question);
    } public void updateQuestion(CustomerQuestion question) {
    // TODO Auto-generated method stub

    }}
    CustomerQuestionDAO.java
    package com.csms.dao;import java.util.ArrayList;
    import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.csms.bean.CustomerQuestion;
    import com.csms.bean.Employee;
    import com.csms.dao.iface.ICustomerQuestionDAO;public class CustomerQuestionDAO extends HibernateDaoSupport implements ICustomerQuestionDAO { public void closeQuestion(Integer questionId) 
    {
      CustomerQuestion question = (CustomerQuestion) this.getHibernateTemplate().get(CustomerQuestion.class, questionId);
            //关闭问题需要把问题的关闭状态设为1
            question.setResultState(1);
            getHibernateTemplate().saveOrUpdate(question);
    } public void deleteQuestion(CustomerQuestion question) {
    // TODO Auto-generated method stub

    } public List<CustomerQuestion> findAllUnResolvedQuestions() {

    String hql="from CustomerQuestion";
    List list = new ArrayList();
    list=this.getHibernateTemplate().find(hql);
    return list;
    } public Employee findEmployee(Integer questionId, Integer employeeType) { CustomerQuestion question = (CustomerQuestion) this.getHibernateTemplate().get(CustomerQuestion.class, questionId);
    System.out.println(question.getCreateEmployee());
    //Employee employee=(Employee) this.getHibernateTemplate().get(CustomerQuestion.class, questionId);
    return null;
    } public CustomerQuestion findQuestionById(Integer id) { CustomerQuestion customerQuestion=(CustomerQuestion) this.getHibernateTemplate().get(CustomerQuestion.class, id);
    return customerQuestion;
    } public void saveQuestion(CustomerQuestion question) {
    this.getHibernateTemplate().save(question);
    } public void updateQuestion(CustomerQuestion question) {
    // TODO Auto-generated method stub

    }
        public Employee findCreateEmployee(Integer questionId) {
            CustomerQuestion question = (CustomerQuestion) this.getHibernateTemplate().load(CustomerQuestion.class,questionId);
            
            Employee employee = new Employee();
            employee=question.getCreateEmployee();
            return employee;
        }    public Employee findTransFromEmployee(Integer questionId) {
            CustomerQuestion question =(CustomerQuestion) this.getHibernateTemplate().load(CustomerQuestion.class,questionId);
            Employee employee = new Employee();
            employee = question.getTransFromEmployee();
            return employee;
        }    public Employee findTransToEmployee(Integer questionId) {
            CustomerQuestion question = (CustomerQuestion) this.getHibernateTemplate().load(CustomerQuestion.class,questionId);
            Employee employee = new Employee();
            employee = question.getTransToEmployee();
            return employee;
        }
    这三个函数只要执行就会报错的 
    }
    麻烦高手看一下 谢谢了
      

  3.   

    看一下applicationContext.xml文件的配置
      

  4.   

    applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
    value="com.mysql.jdbc.Driver">
    </property>
    <property name="url" value="jdbc:mysql://localhost:3306/csms?useUnicode=true&amp;characterEncoding=UTF-8"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
    </bean>
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
    </prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
    </property>
    <property name="mappingResources">  
                     <list>  
                           <value>com/csms/hbm/Employee.hbm.xml</value>  
                           <value>com/csms/hbm/CustomerQuestion.hbm.xml</value>  
                     </list>  
               </property>  

    </bean>

    <!-- 定义员工持久化类 --> 
        <bean id="employee" class="com.csms.bean.Employee" lazy-init="default" autowire="default" dependency-check="default" /> 
    <!-- 定义客户持久化类--> 
        <bean id="customerQuestion" class="com.csms.bean.CustomerQuestion" lazy-init="default" autowire="default" dependency-check="default" /> 

     <!-- 定义员工DAO--> 
    <bean id="employeeDAO" class="com.csms.dao.EmployeeDAO" lazy-init="default" autowire="default" dependency-check="default">
    <property name="sessionFactory">
    <ref local="sessionFactory" /> 
       </property>
       </bean>
    <!-- 定义客户问题DAO--> 
    <bean id="questiondao" class="com.csms.dao.CustomerQuestionDAO" lazy-init="default" autowire="default" dependency-check="default">
    <property name="sessionFactory">
       <ref local="sessionFactory" /> 
      </property>
       </bean>
    <bean id="customerQuestionDAO" class="com.csms.dao.CustomerQuestionDAO" lazy-init="default" autowire="default" dependency-check="default">
    <property name="sessionFactory">
       <ref local="sessionFactory" /> 
      </property>
       </bean>
    <bean id="employeeService" class="com.csms.service.impl.EmployeeService" lazy-init="default" autowire="default" dependency-check="default">
    <property name="employeeDAO">
    <ref bean="employeeDAO"/>
    </property>
       </bean>
    <bean id="customerQuestionService" class="com.csms.service.impl.CustomerQuestionService" lazy-init="default" autowire="default" dependency-check="default">
    <property name="customerQuestionDAO">
    <ref bean="customerQuestionDAO"/>
    </property>
       </bean>
    <bean name="/employeeLogin" class="com.csms.action.EmployeeLoginAction"  lazy-init="default" autowire="default" dependency-check="default">
    <property name="employeeService">
    <ref bean="employeeService"/>
    </property>
        </bean>
      <bean name="/customerService" class="com.csms.action.CustomerServiceAction"  lazy-init="default" autowire="default" dependency-check="default">
    <property name="employeeService">
    <ref bean="employeeService"/>
    </property>
    <property name="questionService">
    <ref bean="customerQuestionService"/>
    </property>
    </bean>
     <bean name="/createQuestion" class="com.csms.action.CustomerServiceAction"  lazy-init="default" autowire="default" dependency-check="default">
    <property name="employeeService">
    <ref bean="employeeService"/>
    </property>
    <property name="questionService">
    <ref bean="customerQuestionService"/>
    </property>
     </bean>
      <bean name="/techSupport" class="com.csms.action.TechSupportAction"  lazy-init="default" autowire="default" dependency-check="default">
    <property name="employeeService">
    <ref bean="employeeService"/>
    </property>
    <property name="questionService">
    <ref bean="customerQuestionService"/>
    </property>
        </bean>
         <bean name="/managerOperate" class="com.csms.action.ManagerOperateAction"  lazy-init="default" autowire="default" dependency-check="default">
    <property name="employeeService">
    <ref bean="employeeService"/>
    </property>
    <property name="questionService">
    <ref bean="customerQuestionService"/>
    </property>
       </bean>
       
     
       
    <bean name="/GoTechSupportView" class="com.csms.action.EmployeeLoginAction"  lazy-init="default" autowire="default" dependency-check="default">
    <property name="employeeService">
    <ref bean="employeeService"/>
    </property>
       </bean>
    </beans>
    麻烦高手......
      

  5.   

    是不是因为load()方法的缘故,如果表中不存在对应id的记录,使用load方法会报错,而使用get方法返回的是null
      

  6.   

    我是根据当前页面传递过来的数据id 来进行get的 id的值我测试过了 是有参数的 
      郁闷 测试了半天还没有调试通 
      

  7.   

    今天刚好学了这个问题!! 完美的解决方法就是些个触发器  在里面获得Session和关闭Session。
    *.do的请求就进入触发器。
    或者lazy="false"也能解决这个问题
      

  8.   

    使用 org.springframework.orm.hibernate3.support.OpenSessionInViewFilter    或者
     org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor
    保持session打开,如果使用OpenSessionInViewFilter   向如下这样filter>    
      <filter-name>OpenSessionInViewFilterfilter-name>   
      <filter-class>   
        org.springframework.orm.hibernate3.support.OpenSessionInViewFilter    
      filter-class>   
      <init-param>   
        <description>   
           默认情况下,这个Filter会在Spring的bean池中找一个叫做sessionFactory的bean。如果使用了其他名字的SessionFactory,则应该在这里    
           指定这个名字。    
        description>   
        <param-name>sessionFactoryBeanName<param-name>   
        <param-value>localSessionFactory<param-value>   
      init-param>   
     
    filter>   
    <filter-mapping>   
      <filter-name>OpenSessionInViewFilterfilter-name>   
      <url-pattern>/*<url-pattern>   
    filter-mapping>   如果使用openSessionInViewInterceptor
    <bean id="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">   
      <property name="sessionFactory" ref="localSessionFactory" />   
    </bean>   
    就需要使用aop
      

  9.   

    spring 有一个配置是关于这个的,可以保持session不关闭,原因在于spring和hibernate的事物有冲突,spring和hibernate都认为各自有一个session,所以spring 会把hiberante的session给关闭了,导致出错,在web.xml中添加一个spring的过滤器,保持 session打开,就行了
      

  10.   

    你的实体类CustomerQuestion中的外键对象createEmployee要new一下。
      

  11.   

    已经解决了 谢谢呵呵 我采用了事务管理
     
    <bean id="employeeService" class="com.csms.service.impl.EmployeeService" lazy-init="default" autowire="default" dependency-check="default">
     <property name="employeeDAO">
      <ref local="employeeDAO" /> 
      </property>
      </bean>
     <!-- 定义客户问题相关业务逻辑
      --> 
     <bean id="customerQuestionService" class="com.csms.service.impl.CustomerQuestionService" lazy-init="default" autowire="default" dependency-check="default">
     <property name="customerQuestionDAO">
      <ref local="customerQuestionDAO" /> 
      </property>
      </bean>
     <!-- 定义Hibernate事务管理器
      --> 
     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="default" autowire="default" dependency-check="default">
     <property name="sessionFactory">
      <ref bean="sessionFactory" /> 
      </property>
      </bean>

    <bean id="employeeServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="default" autowire="default" dependency-check="default">
      <property name="transactionManager">
       <ref bean="transactionManager" /> 
       </property>
      <property name="target">
       <ref local="employeeService" /> 
       </property>
     <property name="transactionAttributes">
      <props>
      <prop key="employeeLogin">PROPAGATION_REQUIRED,readOnly</prop> 
       <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> 
       </props>
       </property>
      </bean>然后要设置<many-to-one 的lazy="false"
    设置 <Set 的 lazy="true"
    否则回报如下错误的
    javax.servlet.ServletException: org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
    org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
    com.csms.processor.EncodingProcessor.process(EncodingProcessor.java:17)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)谢谢帮忙 结贴