获取异常没有Hibernate会话绑定到线程。我正在使用spring mvc

时间:2014-09-12 05:28:41

标签: java spring hibernate spring-mvc

package com.ibs.dao;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ibs.entity.LoginEntity;
@Repository
public class LoginDaoImpl implements LoginDaoInterface {

@Autowired
SessionFactory sessionFactory;


public boolean validateLogin(LoginEntity loginEntity)
{

    String eid=loginEntity.getEid();
    LoginEntity log=(LoginEntity)sessionFactory.getCurrentSession().load(LoginEntity.class, eid);

    if(log.getPassword().equals(loginEntity.getPassword()))
    {
        return true;
    }
    else
    {
        return false;
    }


}

}

我的spring-servlet-config看起来像

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns= .........
..........
    <context:annotation-config/>
    <context:component-scan base-package="com.ibs"/>

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

    <bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
    <!-- <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> -->
    <property name="URL" value="jdbc:-Oracle:thin:@192.168.25.221:1521:xe"/>
    <property name="user" value="lit"/>
    <property name="password" value="lit"/>
</bean>

        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource" ref="dataSource"/>

        <property name="annotatedClasses">
            <list>
                 <value>com.ibs.entity.LoginEntity</value>
            </list>
        </property>

    <property name="hibernateProperties">
         <ref local="hibernateProperties"/>
    </property>
</bean>
<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

错误详情:

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
    org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:700)
    com.ibs.dao.LoginDaoImpl.validateLogin(LoginDaoImpl.java:20)

1 个答案:

答案 0 :(得分:0)

LoginDaoImpl

更改以下行
 LoginEntity log=(LoginEntity)sessionFactory.getCurrentSession().load(LoginEntity.class, eid);

 LoginEntity log=(LoginEntity)sessionFactory.openSession().load(LoginEntity.class, eid);

或者:在hibernate属性中添加键hibernate.current_session_context_class和值Thread的属性。