我正在使用myeclipse,我在spring 3 hibernate 4集成时遇到错误。错误显示“当前线程没有会话”

时间:2013-09-20 08:47:01

标签: java spring hibernate

这是我的applicationContext.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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx">


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation"
            value="file:src/hibernate.cfg.xml">
        </property>
    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" /><bean
        id="StudentDAO" class="org.myeclipse.hibernatespring.StudentDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
</bean>
    <bean id="persistenceLayer"
        class="org.myeclipse.hibernatespring.PersistenceLayer"
        abstract="false" lazy-init="default" autowire="default"
        p:studentDAO-ref="StudentDAO">
    </bean></beans>

我的持久层类,它从applicationcontext.xml

获取StudentDAO的引用
package org.myeclipse.hibernatespring;

public class PersistenceLayer {
private StudentDAO studentDAO;

public StudentDAO getStudentDAO() {
    return studentDAO;
}

public void setStudentDAO(StudentDAO studentDAO) {
    this.studentDAO = studentDAO;
}
public Student listStudent(Integer id)
{   
    return studentDAO.findById(id);
}
}

我的buisnessLogic类创建了一个学生

package org.myeclipse.hibernatespring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class BuisnessLogic {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
Student st=new Student();
st.setId(4);
st.setName("Gaurav");
st.setPer(56.87f);
BeanFactory bf=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
PersistenceLayer pl=(PersistenceLayer) bf.getBean("persistenceLayer");
Student stu=pl.listStudent(2);
System.out.print(stu.getName());
    }

}

这是我运行商务逻辑类

时得到的错误
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" 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:1041)
    at org.myeclipse.hibernatespring.StudentDAO.getCurrentSession(StudentDAO.java:39)
    at org.myeclipse.hibernatespring.StudentDAO.findById(StudentDAO.java:71)
    at org.myeclipse.hibernatespring.PersistenceLayer.listStudent(PersistenceLayer.java:15)
    at org.myeclipse.hibernatespring.BuisnessLogic.main(BuisnessLogic.java:20)

1 个答案:

答案 0 :(得分:0)

首先使用ApplicationContext代替BeanFactory

ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
PersistenceLayer pl=(PersistenceLayer) xml.getBean("persistenceLayer");

在您的配置中,您可以根据<tx:annotation-driven />配置注释驱动的事务,但是您无法使用相应的注释@Transactional注释您的类。添加注释。

@Transactional
public class PersistenceLayer { ... }