简体   繁体   English

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

[英]Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread

I am newbie to Spring and Hibernate. 我是Spring和Hibernate的新手。 I am getting No session found for current thread issue. 我正在为当前线程问题找不到会话。 I tried out looking other thread here but not able to resolve my issue. 我尝试在这里寻找其他线程,但无法解决我的问题。

Perhaps I am lacking some concept. 也许我缺少一些概念。 Please do help to understand what is the reason and how to resolve and please ignore my formatting, I am new on stackoverflow also. 请提供帮助以了解原因以及如何解决,请忽略我的格式化,我也是stackoverflow的新手。 Thanks in advance. 提前致谢。

hibernate.config.xml 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 Class 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;
    }

}

Use openSession method of sessionFactory before creating query: 创建查询之前,请使用sessionFactory的openSession方法:

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

Don't forget to close the session after your work is done. 完成工作后,别忘了关闭会话。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Estado HTTP 500-请求处理失败; 嵌套的异常是org.hibernate.HibernateException:当前会话找不到会话 - Estado HTTP 500 - Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread HTTP状态500-请求处理失败; 嵌套的异常是org.hibernate.HibernateException:没有绑定到线程的Hibernate会话 - HTTP Status 500 - Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread 嵌套的异常是org.hibernate.HibernateException:当前会话addDepartment控制器找不到会话 - nested exception is org.hibernate.HibernateException: No Session found for current thread addDepartment controller Hibernate 4:org.hibernate.HibernateException:当前会话找不到会话 - Hibernate 4 : org.hibernate.HibernateException: No Session found for current thread org.hibernate.HibernateException:使用tx:advise找不到当前线程的会话 - org.hibernate.HibernateException: No Session found for current thread with tx:advise org.hibernate.HibernateException: 没有找到当前线程 5 的会话 - org.hibernate.HibernateException: No Session found for current thread 5 由于&#39;org.hibernate.HibernateException所需的@Transactional注释:找不到当前线程的异常&#39;异常 - @Transactional annotation required due to 'org.hibernate.HibernateException: No Session found for current thread' exception Estado HTTP 500-请求处理失败; 嵌套的异常是org.hibernate.HibernateException:如果没有活动的事务,createQuery无效 - Estado HTTP 500 - Request processing failed; nested exception is org.hibernate.HibernateException: createQuery is not valid without active transaction Spring + Hibernate应用程序中的问题:org.hibernate.HibernateException:找不到当前线程的Session - Problems in Spring + Hibernate application: org.hibernate.HibernateException: No Session found for current thread Spring Security authenticationFailure:错误org.hibernate.HibernateException:没有找到当前线程的会话 - Spring Security authenticationFailure : error org.hibernate.HibernateException: No Session found for current thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM