当前位置: 首页 > 知识库问答 >
问题:

Estado HTTP 500-请求处理失败;嵌套异常是org.hibernate.hibernateException:没有找到当前线程的会话

柯易安
2023-03-14

我是用Spring和hibernate的新手,我一步一步地跟着教程但是我得到这个会话错误但是不知道具体怎么修复它,有人能帮我吗?非常感谢。我编写的contintuacion通过我编写的代码给出了错误。

grave:El servlet.service()para El servlet[springapp]en El contexto con ruta[/springapp]lanzóla excepción[请求处理失败;嵌套异常是org.hibernate.hibernateException:未找到当前线程的会话]con causa raíz org.hibernate.hibernateException:未找到当前线程的会话在com.altobri.conta.service.empresasDaoImpl.java:43),在com.altobri.conta.controller.empresasServiceImpl.listemPresas(EmpresasServiceImpl.java:47),在com.altobri.controller.empresalistaController.empresalistaController.list(empresalistaController.java:30),在sun.reflect.nativeMethodAccessorImpl.Invoke0(原生方法),在va:43)在java.lang.reflect.method.invoke(method.java:606)在org.springframework.web.bind.annotation.support.handlerMethodInvoker.invokeHandlerMethod(handlerMethodInvoker.java:176)在org.springframework.web.servlet.mvc.annotationMethodHandlerAdapter.invokeHandlerMethod(annotationMethodHandlerAdapter.java:440)在(dispatcherservlet.java:925)在org.springframework.web.servlet.dispatcherservlet.doService(dispatcherservlet.java:856)在org.springframework.web.servlet.frameworkservlet.processRequest(frameworkservlet.java.936)在org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.javax.http.http.httpservlet)在服务(httpservlet.java:728)在org.apache.catalina.core.applicationFilterChain.InternaldoFilter(applicationFilterChain.java:305)在org.apache.catalina.core.applicationFilterChain.doFilter(applicationFilterChain.java:210)在org.apache.catalina.core.applicationFilterChain.doFilter(applicationFilterChain.java:210)在.apache.catalina.core.standardWrapperValve.invoke(StandardWrapperValve.java:222)在org.apache.catalina.core.standardContextValve.invoke(StandardContextValve.java:123)在org.apache.catalina.authenticator.authenticator.Authenticatorbase.invoke(StandardContextValve.invoke(StandardContextValve.java:502)在ve.java:953)在或在org.apache.catalina.core.standardEngineValve.invoke(StandardEngineValve.java:118)在org.apache.catalina.connector.coyoteAdapter.Service(CoyoteAdapter.java:408)在org.apache.coyote.http11.abstractThttp11Processor.Process(AbstractThttp11Processor.process(AbstractThttp11Processor.java:1041)在org.apache.coyote.AbstractProtocol runworker(threadpoolexecutor.java:1145)在java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615)在java.lang.thread.run(thread.java:744)

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">
 <display-name>Spring3MVC</display-name>
 <welcome-file-list>
     <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

 <servlet>

springapp-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:p="http://www.springframework.org/schema/p"
   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-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:component-scan base-package="com.companyname.springapp.web" />
   <context:component-scan base-package="com.springHibernate" />
   <context:component-scan base-package="com.altobri.conta.*" />

 <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>


     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/empresas" />
    <property name="username" value="root" />
    <property name="password" value="" />
  </bean>

  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
     <property name="annotatedClasses">
            <list>
                <value>com.altobri.conta.model.Empresas</value>
            </list>
        </property>
    <property name="hibernateProperties">
      <props>
        <prop 
         key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        <prop key="hibernate.show_sql">true</prop>
      </props>
    </property>
  </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
    p:sessionFactory-ref="sessionFactory">
  </bean>
</beans>



 <servlet-name>springapp</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

empresalistaController.java

package com.altobri.conta.controller;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

import com.altobri.conta.model.Empresas;
import com.altobri.conta.service.*;

@Controller
public class empresaListaController {

    @Autowired
    private EmpresasService empresasService;
    protected final Logger logger = Logger.getLogger(getClass());


    @RequestMapping("/empresaLista")
    // @RequestMapping(method = RequestMethod.GET)
    public String list(Model model) {

      List<Empresas> empresas = empresasService.listEmpresas();
      model.addAttribute("empresalist", empresas);
      return "empresaLista";
    }
}
package com.altobri.conta.dao;

import com.altobri.conta.model.Empresas;
import java.util.List;

public interface EmpresasDAO {
    void persistEmpresas(Empresas empresas);

    Empresas findEmpresasById(int clave);

    void updateEmpresas(Empresas empresas);

    void deleteEmpresas(Empresas empresas);

    public List<Empresas> listEmpresas();

}
package com.altobri.conta.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.altobri.conta.model.Empresas;

@Repository("empresasDAO")
public class EmpresasDAOImpl implements EmpresasDAO {

    @Autowired
    public SessionFactory sessionFactory;

    @Override
    public void persistEmpresas(Empresas empresas) {
        sessionFactory.getCurrentSession().persist(empresas);
    }

    @Override
    public Empresas findEmpresasById(int clave) {
        return (Empresas) sessionFactory.getCurrentSession().get(
                Empresas.class, clave);
    }

    @Override
    public void updateEmpresas(Empresas empresas) {
        sessionFactory.getCurrentSession().update(empresas);

    }

    @Override
    public void deleteEmpresas(Empresas empresas) {
        sessionFactory.getCurrentSession().delete(empresas);

    }

    @Override
    public List<Empresas> listEmpresas() {
        Session session = sessionFactory.getCurrentSession();
        List empresas = session.createQuery("from Empresas").list();
        // TODO Auto-generated method stub
         return empresas;
    }

}
package com.altobri.conta.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "EMPRESAS")
public class Empresas {

    @Id
    @Column(name = "CLAVE", nullable = false)
    private int clave;

    @Column(name = "NOMBRE", nullable = false)
    private String nombre;


    public Empresas() {


    }


    public int getClave() {
        return clave;
    }


    public void setClave(int clave) {
        this.clave = clave;
    }


    public String getNombre() {
        return nombre;
    }


    public void setNombre(String nombre) {
        this.nombre = nombre;
    }



}

empresasservice.java

package com.altobri.conta.service;



import java.util.List;

import com.altobri.conta.model.Empresas;

public interface EmpresasService {

    void persistEmpresas(Empresas empresas);

    Empresas findEmpresasById(int clave);

    void updateEmpresas(Empresas empresas);

    void deleteEmpresas(Empresas empresas);

    public List<Empresas> listEmpresas();
}

empresasServiceImpl.java

package com.altobri.conta.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.altobri.conta.dao.EmpresasDAO;
import com.altobri.conta.model.Empresas;

@Service("empresasService")
public class EmpresasServiceImpl implements EmpresasService{

    @Autowired
    EmpresasDAO empresasDAO;

    @Override
    @Transactional
    public void persistEmpresas(Empresas empresas) {
        empresasDAO.persistEmpresas(empresas);

    }

    @Override
    @Transactional
    public void updateEmpresas(Empresas empresas) {
        empresasDAO.updateEmpresas(empresas);

    }
    @Override
    @Transactional
    public Empresas findEmpresasById(int clave) {
        return empresasDAO.findEmpresasById(clave);
    }

    @Override
    @Transactional
    public void deleteEmpresas(Empresas empresas) {
        empresasDAO.deleteEmpresas(empresas);

    }

    @Override
    public List<Empresas> listEmpresas() {
        // TODO Auto-generated method stub
        return empresasDAO.listEmpresas();
    }

}

共有1个答案

张成济
2023-03-14

您需要更新springapp-servlet.xml下面的值

对会话上下文使用以下值。

<prop key="hibernate.current_session_context_class">thread</prop>

thread是org.hibernate.context.internal.ThreadLocalSessionContext的缩写

请查看此链接以获得更详细的信息。

http://docs.jboss.org/hibernate/orm/4.1/manual/en-us/html_single/#体系结构-current-session

 类似资料: