0

我做了很多研究,但我没有在这里得到解决方案,我的错误是

org.hibernate.HibernateException: No Session found for current thread
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:978)
    at dao.GenericDaoImpl.save(GenericDaoImpl.java:42)
    at dao.UserDao.save(UserDao.java:18)
    at controllers.UserController.userRegistrationSubmit(UserController.java:36)
    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.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

我的 SpringConfig 文件是

<?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:tx="http://www.springframework.org/schema/tx"
    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-2.5.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">


<context:component-scan base-package="controllers,dao" />
<tx:annotation-driven transaction-manager="transactionManager"/>



  <!-- 
     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
        <tx:attributes>  
            <tx:method name="*"  />  
        </tx:attributes>  
    </tx:advice>     -->

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

 <bean
    id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="configLocation">    
        <value>
            classpath:hibernate.cfg.xml
        </value>
    </property>

    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
                <prop key="hibernate.show_sql">true</prop>
                <!-- <prop key="hibernate.c3p0.min_size">5</prop>
                <prop key="hibernate.c3p0.max_size">20</prop>
                <prop key="hibernate.c3p0.timeout">300</prop>
                <prop key="hibernate.c3p0.max_statements">50</prop>
                <prop key="hibernate.c3p0.idle_test_period">3000</prop>  -->
            </props>
     </property>
</bean>    

<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/foodcartdb" />
    <property name="username" value="root" />
    <property name="password" value="root" />

</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
     <property name="sessionFactory" ref="sessionFactory"/>
</bean>

</beans>

这是我保存数据的代码

package dao;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
//import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class GenericDaoImpl<T> implements GenericDao<T> {

    /*public GenericDaoImpl(){
        //Do Also Nothing here ha ha ha
    }
    //@Autowired
    public GenericDaoImpl(SessionFactory sessionFactory){
        super.setSessionFactory(sessionFactory);
    }*/
    /*@Autowired
    protected void init(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }*/

    @Autowired
    @Qualifier("sessionFactory")
    private SessionFactory sessionFactory;

    public Session getSession() {
        return sessionFactory.getCurrentSession();
    }
    @Override
    public void save(T t) {
        // TODO Auto-generated method stub
        //getHibernateTemplate().save(t);
        this.sessionFactory.getCurrentSession().save(t);
    }

    @Override
    public void update(T t) {
        // TODO Auto-generated method stub
        //getHibernateTemplate().update(t);
        getSession().update(t);
    }

    @Override
    public void delete(T t) {
        // TODO Auto-generated method stub
        //getHibernateTemplate().delete(t);
        getSession().delete(t);
    }

}

请有人帮我解决我哪里出错了,我已经花了将近半天的时间。

4

3 回答 3

1

尝试使用@Repository注释而不是@Configurationand @EnableTransactionManagement...

@Repository
public class GenericDaoImpl<T> implements GenericDao<T> {
    // code goes here
}

@Repository API文档在这里

在 Spring 2.0 及更高版本中,@Repository 注释是任何满足存储库角色或原型(也称为数据访问对象或 DAO)的类的标记。此标记的用途之一是异常的自动翻译。

春季文档在这里

于 2013-08-01T23:44:19.877 回答
1

首先,类上的组合@Configuration@EnableTransactionManagement注释GenericDaoImpl是多余的,因为您已经在 Spring XML 配置文件中启用了注释驱动的事务管理。

其次,GenericDaoImpl应注释为@Repository

第三,你没有发布你的UserDao课程。它继承GenericDaoImpl吗?如果是这样,请考虑制作GenericDaoImpl抽象类(然后唯一的@Repository应该是UserDao单独的)。

第四,为了获得会话,您必须将方法注释为@Transactional或使用sessionFactory.openSession(). Using@Transactional让 Spring 处理处理打开/关闭会话的样板代码,但强制您在方法的整个范围内拥有一个当前的单个事务。为自己打开一个会话可以更好地控制会话管理,但相当乏味。

第五,如果UserDao继承GenericDaoImpl@Transactionalsave方法上使用是没有用的,因为(默认情况下)此类注释仅适用于通过 bean 接口执行的方法调用。由于无论如何在这里使用继承都是一种不好的做法,因此您应该将其删除并在其session.save(t)内部调用UserDao

最后也是最不重要的一点是,最好使用javax.inject注释进行依赖注入,例如@Inject@Named(分别替换@Autowire@Qualifier)。

于 2013-08-02T00:09:19.477 回答
0

我认为你应该@Transactional在你的 DAO 类和@Configuration/@EnableTransactionManagement在单个配置类上使用,而不是在每个 DAO 上使用。

于 2013-08-01T22:03:50.847 回答