Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.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
Hibernate Spring集成错误:没有绑定到线程的Hibernate会话_Spring_Hibernate - Fatal编程技术网

Hibernate Spring集成错误:没有绑定到线程的Hibernate会话

Hibernate Spring集成错误:没有绑定到线程的Hibernate会话,spring,hibernate,Spring,Hibernate,我在集成Hibernate和Spring时遇到了一个问题。当我运行程序时,我将得到没有绑定到线程的Hibernate会话,并且配置不允许在这里创建非事务会话 我查看了论坛,但大多数都与事务管理器或缺少上下文有关:组件扫描。但我有 package com.cmpt.project.persistence; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.be

我在集成Hibernate和Spring时遇到了一个问题。当我运行程序时,我将得到
没有绑定到线程的Hibernate会话,并且配置不允许在这里创建非事务会话

我查看了论坛,但大多数都与事务管理器或缺少上下文有关:组件扫描。但我有

package com.cmpt.project.persistence;

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

@Repository
public class WarehouseDao {


    private SessionFactory sessionFactory;

    @Autowired
    public WarehouseDao (SessionFactory sessionFactory){
        this.sessionFactory = sessionFactory;
    }
    public Session currentSession() {
        return sessionFactory.getCurrentSession();
    }
}
这是我的配置文件spring-hibernate.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:annotation-config />    

    <context:component-scan base-package="com.cmpt.project.persistence"/>

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/spring_demo"></property>
        <property name="username" value="username"></property>
        <property name="password" value="password"></property>
        <property name="initialSize" value="5"></property>
        <property name="maxActive" value="10"></property>
    </bean>


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="packagesToScan"
            value="com.cmpt.project.model">
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>

    <!-- <bean
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor">
    </bean> -->
</beans>

如上所示,我甚至没有更新数据库。我甚至无法获取会话对象。请提供帮助,谢谢。

尝试设置hibernate属性:

<prop key="hibernate.current_session_context_class">thread</prop>
线程
将会话绑定到线程


希望这有帮助。

您必须添加事务管理器并进行适当的事务设置。Spring使用此信息,以便知道何时启动事务,从而获得会话。您需要使用
@Transactional
注释装饰Hibernate DAO实现类。我在WarehouseDao类中添加了@Transactional,并进行了设置,但同样的问题仍然存在是的,这解决了问题。非常感谢你。我可以问一下它为什么起作用吗?为什么它以前不能工作?教科书和教程视频都没有提到这个设置。另外,我计划使用多线程更新表。它会干扰多个线程吗?使用它,您将Hibernate会话绑定到当前线程,因此如果有多个线程,每个线程都有自己的会话。注意事务管理以保持数据的一致性,您应该不会有任何问题。明白了。谢谢。那么会话工厂是使用配置创建的单例,它使用多线程生成多个会话?
<prop key="hibernate.current_session_context_class">thread</prop>