[英]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.