请求处理失败;嵌套异常是org.hibernate.HibernateException:找不到当前线程的Session

时间:2015-08-06 04:25:11

标签: java spring hibernate spring-mvc

我是Spring和Hibernate的新手。我没有找到任何会话 当前线程问题。我试着在这里寻找其他线程但不是 能够解决我的问题。

也许我缺乏一些概念。请做 帮助理解是什么原因以及如何解决和取悦 忽略我的格式,我也是stackoverflow的新手。谢谢 提前。

hibernate.config.xml

<!-- Scan classpath for annotations (eg: @Service, @Repository etc) -->
<context:annotation-config/>

<bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:database.properties</value>
    </property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
      p:username="${jdbc.username}" p:password="${jdbc.password}"/>


<bean id="mySessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="com.shop.model"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>

        </props>
    </property>
</bean>
<tx:annotation-driven proxy-target-class="true"
                      transaction-manager="txManager"/>
<bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory"/>
</bean>

UserDaoImpl类

package com.shop.dao.impl;

@Repository
public class UserDaoImpl implements IUserDao {

    private static final Logger LOG =
            Logger.getLogger(UserDaoImpl.class);

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public boolean checkUser(String email, String password) {
        boolean userFound = false;
        String hql = "from UserInfo where email =? and password=?";
        LOG.info(sessionFactory.getCurrentSession().createQuery(hql));
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setParameter(0, email);
        query.setParameter(1, password);
        List userList = query.list();

        if ((userList != null) && (userList.size() 0)){
            userFound = true;
        }
        return userFound;
    }

}

1 个答案:

答案 0 :(得分:0)

在创建查询之前使用sessionFactory的openSession方法:

Session session = sessionFactory.openSession();
Query query = session.createQuery(hql);

完成工作后别忘了关闭会话。