例外:没有Hibernate Session绑定到线程,并且配置不允许在此处创建非事务性会话

时间:2018-11-27 17:05:53

标签: java spring hibernate

部署应用程序后出现错误。我已经检查了类似的问题,但仍然无法解决该错误。我已经将@Transactional用于服务类以及

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
    org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:687)
    com.utd.dao.CustomerDaoImpl.getAllCustomer(CustomerDaoImpl.java:22)
    com.utd.service.CustomerManagerImpl.getAllCustomer(CustomerManagerImpl.java:23)
    com.utd.controller.CustomerController.listCustomers(CustomerController.java:22)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)

我有以下项目文件: 控制器:

    @Controller
public class CustomerController {
    @Autowired
    private CustomerManager customerManager;

    @RequestMapping(value="/", method= RequestMethod.GET)
    public String listCustomers(ModelMap map){
        map.addAttribute("customer", new Customer());
        map.addAttribute("customerList", customerManager.getAllCustomer());
        return "CustomerList";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addCustomer(@ModelAttribute(value="customer") Customer customer, BindingResult result)
    {
        customerManager.addCustomer(customer);
        return "redirect:/";
    }
    @RequestMapping("/delete/{customerId}")
    public String deleteEmplyee(@PathVariable("customerId") Integer customerId)
    {
        customerManager.deleteCustomer(customerId);
        return "redirect:/";
    }

    public void setCustomerManager(CustomerManager customerManager) {
        this.customerManager = customerManager;
    }
}

道课

public class CustomerDaoImpl implements CustomerDao{
    @Autowired

    private SessionFactory sessionFactory;

    public void addCustomer(Customer customer) {
        this.sessionFactory.getCurrentSession().save(customer);
    }

    @SuppressWarnings("unchecked")
    public List<Customer> getAllCustomer() {
        return this.sessionFactory.getCurrentSession().createQuery("from Customer").list();
    }

    public void deleteCustomer(Integer customerId) {
        Customer customer = (Customer) sessionFactory.getCurrentSession().load(Customer.class, customerId);
        if (null != customer) {
            this.sessionFactory.getCurrentSession().delete(customer);
        }
    }

}

Dto:

@Entity
@Table(name="Customer")
public class Customer {
    @Id
    @Column(name="ID")
    @GeneratedValue
    private int id;

   @Column(name="NAME")
   private String name;

    @Column(name="CONTACT")
    private String contact;

    @Column(name="EMAIL")
    private String email;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

customer-servlet.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"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:jee="http://www.springframework.org/schema/jee"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:util="http://www.springframework.org/schema/util"
           xsi:schemaLocation="http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-2.5.xsd
             http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
            http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd">
        <context:annotation-config />
        <context:component-scan base-package="com.utd.controller" />
        <bean id="jspViewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass"
                      value="org.springframework.web.servlet.view.JstlView"></property>
            <property name="prefix" value="/WEB-INF/view/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        <bean id="messageSource"
              class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
            <property name="basename" value="classpath:messages"></property>
            <property name="defaultEncoding" value="UTF-8"></property>
        </bean>
        <bean id="propertyConfigurer"
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
              p:location="/WEB-INF/jdbc.properties"></bean>
        <bean id="dataSource"
              class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
              p:driverClassName="${jdbc.driverClassName}"
              p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
              p:password="${jdbc.password}"></bean>
        <bean id="sessionFactory"
              class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="configLocation">
                <value>classpath:hibernate.cfg.xml</value>
            </property>
            <property name="configurationClass">
                <value>org.hibernate.cfg.AnnotationConfiguration</value>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                    <prop key="hibernate.show_sql">true</prop>
                </props>
            </property>
        </bean>
        <bean id="customerDao" class="com.utd.dao.CustomerDaoImpl"></bean>
        <bean id="customerManager" class="com.utd.service.CustomerManagerImpl"></bean>
        <tx:annotation-driven />
        <bean id="transactionManager"
              class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
    </beans>

dispatcher-servlet.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <mvc:annotation-driven />
    <context:component-scan base-package="com.utd" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
    </bean>


</beans>

Manager类的实现

    package com.utd.service;

import com.utd.dao.CustomerDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.utd.dto.Customer;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class CustomerManagerImpl implements CustomerManager {
    @Autowired
    private CustomerDao customerDao;

    @Transactional
    public void addCustomer(Customer customer) {
        customerDao.addCustomer(customer);
    }

    @Transactional
    public List<Customer> getAllCustomer() {
        return customerDao.getAllCustomer();
    }

    @Transactional
    public void deleteCustomer(Integer customerId) {
        customerDao.deleteCustomer(customerId);
    }

    public void setCustomerDao(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }
}

我想知道如何解决该错误。

0 个答案:

没有答案