Hibernate Spring Integration Error:没有Hibernate会话绑定到线程

时间:2014-03-26 08:19:35

标签: spring hibernate

集成Hibernate和Spring时遇到问题。当我运行该程序时,我将获得No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

我检查了论坛,但其中大部分与事务管理器或缺少上下文有关:组件扫描。但我知道了。

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>

这是我的主要

package com.cmpt.project.app;

import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cmpt.project.model.Customer;
import com.cmpt.project.persistence.WarehouseDao;

public class ProjectDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-hibernate.xml");
        WarehouseDao dao = ctx.getBean("warehouseDao", WarehouseDao.class);

        Session session = dao.currentSession();
    }

}

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

1 个答案:

答案 0 :(得分:1)

尝试设置hibernate属性:

<prop key="hibernate.current_session_context_class">thread</prop>

将会话绑定到线程。

希望这有帮助。