Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HibernateeException:未找到当前线程的会话_Java_Spring_Hibernate - Fatal编程技术网

Java HibernateeException:未找到当前线程的会话

Java HibernateeException:未找到当前线程的会话,java,spring,hibernate,Java,Spring,Hibernate,我已经阅读并找到了许多类似问题的答案,如 我已尝试添加tx:annotation-driven-transaction-manager=“transactionManager”>** 但结果是,Servlet mvc dispatcher抛出异常的HTTP状态500-Servlet.init()出现错误 我怎样才能解决这个问题。(Spring 3.2.0+Hybernate 4.2.0.最终版) 细节 web.xml <web-app version="2.4" xmlns="h

我已经阅读并找到了许多类似问题的答案,如

我已尝试添加tx:annotation-driven-transaction-manager=“transactionManager”>** 但结果是,Servlet mvc dispatcher抛出异常的HTTP状态500-Servlet.init()出现错误

我怎样才能解决这个问题。(Spring 3.2.0+Hybernate 4.2.0.最终版)

细节

web.xml

<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
}

控制器

@Controller
@RequestMapping("/")
public class HelloController {

    private HibernateSpitterDao hibernateSpitterDao;

    @Autowired
    public HelloController(HibernateSpitterDao hibernateSpitterDao) {
        this.hibernateSpitterDao =  hibernateSpitterDao;
    }


    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello world!");
        model.addAttribute("Student",new Student());
        return "hello";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String printWelcome1(@Valid Student student,
                                BindingResult bindingResult) {
        hibernateSpitterDao.savestudent(student);
        return "hello";
    }
}
JSP


${message}
开始
登录

密码

登记处
您在saveStudent dao方法上有@Transactional注释,但也尝试在dao方法中手动创建一个事务执行类似操作:

@Transactional(readOnly = false)
public void savestudent(Student student) {
    Session currentSession = sessionFactory.getCurrentSession();
    currentSession.save(student);
}

还要确保dao类位于组件扫描标记中指定的包之内或之下,否则spring将无法获取注释。或者,您可以在spring连接中为dao添加一个bean定义,该定义还允许spring对其注释进行处理。

您需要更新您的
Dispatcher servlet.xml中的值

对会话上下文使用以下值

<prop key="hibernate.current_session_context_class">thread</prop>
线程
thread是org.hibernate.context.internal.ThreadLocalSessionContext的缩写

请查看此链接以了解更多详细信息

解决方案 1.删除存储库类中的构造函数。
2.删除存储库类中的事务。

我也遇到了同样的问题

我使用的是基于注释的配置,我已经配置了sessionfactory、datasource和事务管理器。但我没有在AppConfig类中给出@EnableTransactionManagement注释

添加事务注释后,代码如下所示

@Configuration
@ComponentScan("com.bmp.*")
@EnableWebMvc
@PropertySource("classpath:${env}.properties")
@EnableTransactionManagement
public class AppConfig {
-----
}

上面的注释解决了我的问题

请在dao层中使用@Repository注释,并在save student上使用@Transnational注释

我尝试了您的版本,但结果我也遇到了相同的错误。您的dao类在com.springapp.mvc包中吗?如果我在运行带有此更改的代码时没有将它所在的包添加到中,则它对我有效。我在JBoss AS7U中使用Spring 3.2.2版本和Hibernate 4.2 Final来测试我的项目吗?是的,我使用了上面的文件。你能发送这些文件和pom文件吗?我想再次检查您是否满意。问题出在事务中。另一个问题是5年前存储中的构造函数看起来需要删除该问题。)
@Transactional(readOnly = false)
public void savestudent(Student student) {
    Session currentSession = sessionFactory.getCurrentSession();
    currentSession.save(student);
}
<prop key="hibernate.current_session_context_class">thread</prop>
@Configuration
@ComponentScan("com.bmp.*")
@EnableWebMvc
@PropertySource("classpath:${env}.properties")
@EnableTransactionManagement
public class AppConfig {
-----
}