当前位置: 首页 > 面试题库 >

Spring-Hibernate-当前会话未找到会话

公良昕
2023-03-14
问题内容

我有一个使用spring和hibernate的java stuts2 Web应用程序。

我越来越org.hibernate.HibernateException: No Session found for current thread

SpringBean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    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
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

    <context:component-scan base-package="org.rohith" />
    <context:annotation-config />
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/company" />
        <property name="username" value="root" />
        <property name="password" value="password" />
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

        <property name="dataSource">
            <ref bean="dataSource" />
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>

        <property name="mappingResources">
            <list>
                <value>hibernate.cfg.xml</value>
            </list>
        </property>

    </bean>
    <!-- <tx:annotation-driven/> -->
    <bean id = "transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name = "sessionFactory" ref = "sessionFactory" />
    </bean>

    <bean id="customerDaoImpl" class="org.rohith.dao.impl.CustomerDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="customerServiceImpl" class="org.rohith.service.impl.CustomerServiceImpl">
        <property name="customerDaoImpl" ref="customerDaoImpl"/>
    </bean>

</beans>

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
   <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>  
        <!-- Names the annotated entity class -->
        <mapping class="org.rohith.model.Customer"/>    
    </session-factory>  
</hibernate-configuration>

CustomerServiceImpl.java

package org.rohith.service.impl;

import org.rohith.dao.impl.CustomerDaoImpl;
import org.rohith.model.Customer;
import org.rohith.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDaoImpl customerDaoImpl;

    @Override
    public void saveCustomer(Customer customer) {
        customerDaoImpl.saveCustomer(customer);
    }
    public CustomerDaoImpl getCustomerDaoImpl() {
        return customerDaoImpl;
    }
    public void setCustomerDaoImpl(CustomerDaoImpl customerDaoImpl) {
        this.customerDaoImpl = customerDaoImpl;
    }

}

CustomerDaoImpl.java

package org.rohith.dao.impl;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.rohith.dao.CustomerDao;
import org.rohith.model.Customer;


public class CustomerDaoImpl   implements CustomerDao {

    private SessionFactory sessionFactory;

    @Override
    public void saveCustomer(Customer customer) {
        Session session = getSession();
        session.clear();
        try {
                session.saveOrUpdate(customer);
                session.flush();
        } catch (Throwable e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public SessionFactory getSessionFactory() {
        return this.sessionFactory;
    }

    public Session getSession() throws HibernateException {
        Session sess = getSessionFactory().getCurrentSession();
        if (sess == null) {
            sess = getSessionFactory().openSession();
        }
//      Session sess = getSessionFactory().openSession();
        return sess;
    }

}

CustomerAction.java

public class CustomerAction extends ActionSupport{
    private String name;
    private String addr1;
    private String addr2;
    private String city;
    private String state;

    private CustomerServiceImpl customerServiceImpl;

//Getters and setters

    public String execute(){
        Customer cust= new Customer();
        cust.setName(name);
        cust.setAddress1(addr1);
        cust.setAddress2(addr2);
        cust.setCity(city);
        cust.setState(state);
        System.out.println(name);
        WebApplicationContext webApplicationContext = WebApplicationContextUtils
                .getRequiredWebApplicationContext(getRequest().getSession()
                        .getServletContext());
        customerServiceImpl = (CustomerServiceImpl) webApplicationContext.getBean("customerServiceImpl");
        customerServiceImpl.saveCustomer(cust);
        //saveCust(cust);
        return "success";
    }
protected HttpServletRequest getRequest() {
        return ServletActionContext.getRequest();
    }
protected HttpServletResponse getResponse() {
        return ServletActionContext.getResponse();
    }
}

我得到的例外

org.hibernate.HibernateException: No Session found for current thread
    org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
    org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:978)
    org.rohith.dao.impl.CustomerDaoImpl.getSession(CustomerDaoImpl.java:33)
    org.rohith.dao.impl.CustomerDaoImpl.saveCustomer(CustomerDaoImpl.java:16)
    org.rohith.service.impl.CustomerServiceImpl.saveCustomer(CustomerServiceImpl.java:18)
    org.rohith.CustomerAction.execute(CustomerAction.java:36)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)

问题答案:

您在Spring配置中指定了一个事务管理器,但是没有关于何时或何处应用事务的配置。

在您的 SpringBean.xml中, 您应该取消注释<tx:annotation-driven/>

<tx:annotation-driven transaction-manager="transactionManager"/>

然后,您应该将CustomerServiceImpl.saveCustomer方法注释为@Transactional

@Service
public class CustomerServiceImpl implements CustomerService {

    ...

    @Override
    @Transactional
    public void saveCustomer(Customer customer) {
        customerDaoImpl.saveCustomer(customer);
    }

    ...
}


 类似资料:
  • 问题内容: 我有一个使用spring和hibernate的java stuts2 Web应用程序。 我越来越。 SpringBean.xml hibernate.cfg.xml CustomerServiceImpl.java CustomerDaoImpl.java CustomerAction.java 我得到的例外 问题答案: 您在Spring配置中指定了一个事务管理器,但是没有关于何时或何

  • 我得到以下错误 服务级别 刀类钻头 这个在我application-context.xml 有人能指出为什么我会得到下面的错误吗?

  • 我试图在一个演示的独立应用程序中使用Spring,使用DAO层和服务层来使用委托的Hibernate事务。 我已经正确地设置了配置,并且我已经对DAO方法上使用@Transactional注释进行了单元测试,测试结果表明它可以正常工作,但是当我将这个注释移动到服务层时,我得到了一个: 我提供了代码中最相关的部分,希望您能给我一个提示,让我了解这里发生了什么。 在GenericDaoHibernat

  • 问题内容: 我收到以下错误 服务等级 DAO类 我在我的application-context.xml中有这个 谁能发现我为什么出现以下错误? 问题答案: 您使用@Transactional为Dao类添加了注释,但没有为服务类添加注释。该行: 要求您进行交易。 您可以通过将@Transactional批注添加到ProfileService类,或者只添加registerVisitor()方法来解决此

  • 问题内容: 我在Spring3和Hibernte4中遇到上述异常 以下是我的bean xml文件 我的BaseDAO类看起来像这样 以下代码在标题中引发异常 有谁知道如何解决这个问题? 问题答案: 仅在交易范围内有意义。 您需要声明一个适当的事务管理器,划分事务边界并在其中进行数据访问。例如,如下: 。 也可以看看: 10.交易管理 13.3hibernate

  • 问题内容: 我正在尝试使用Spring 3.1和Hibernate 4设置项目。我一直在在线关注一些教程。我收到一个奇怪的错误,根据Spring论坛,应该使用Spring 3.1进行修复。 Spring Bug Tracker 当我的服务调用时,它将引发以下异常: *编辑:根据Spring 3.1事务文档更新了spring-dao.xml 。我尝试用org.apache.commons.dbcp.