当前位置: 代码迷 >> J2EE >> org.hibernate.HibernateException: No Session found for current thread,该如何处理
  详细解决方案

org.hibernate.HibernateException: No Session found for current thread,该如何处理

热度:207   发布时间:2016-04-17 23:35:14.0
org.hibernate.HibernateException: No Session found for current thread
这是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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
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.0.xsd 
                           http://www.springframework.org/schema/tx  
                           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
                           http://www.springframework.org/schema/aop  
                           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
                              

<!-- 数据元配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <property name="url" value="jdbc:mysql://localhost:3306/callsystem"></property>
  <property name="username" value="root"></property>
  <property name="password" value="521958k42jv"></property>   
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
  <property name="mappingResources">
    <list>
      <value>/hbm/xml/Group/user.hbm.xml</value>
      <value>/hbm/xml/Group/userMap.hbm.xml</value>
      <value>/hbm/xml/Group/set_meal.hbm.xml</value>
      <value>/hbm/xml/Group/card.hbm.xml</value>
      <value>/hbm/xml/Group/history.hbm.xml</value>
      <value>/hbm/xml/Group/action.hbm.xml</value>
    </list>
  </property>
  <property name="hibernateProperties">
    <props>
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
      <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
      <prop key="hibernate.show_sql">false</prop>
      <prop key="hibernate.format_sql">true</prop>
      <!-- <prop key="hibernate.current_session_context_class"> thread</prop> -->
    </props>
  </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
    <tx:attributes> 
        <tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="create*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true" /> 
    </tx:attributes> 
    </tx:advice>  

<aop:config proxy-target-class="true"> 
    <aop:pointcut id="productServiceMethods" expression="execution(* user.hibernate..*.*(..))" /> 
        <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/> 
</aop:config>       
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    <bean id="userMapDao" class="userMap.hibernate.UserMapDao">
      <property name="sessionFactory" ref="sessionFactory">
      </property>
</bean>

<bean id="userDao" class="user.hibernate.UserDao">
  <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="printUser" class="global.other.Print">
  
</bean>  

<bean id="cardDao" class="card.hibernate.CardDao">
  <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
    
    <bean id="historyDao" class="history.hibernate.HistoryDao">
      <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <bean id="actionDao" class="action.hibernate.ActionDao">
      <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>


userDao


public class UserDao {
private SessionFactory sessionFactory;
        public User getUser(String name,String password)
{
if(this.isHaveUser(name)&&this.isCorrectPwd(name, password))
{
Query query=this.sessionFactory.getCurrentSession().createQuery("from User u where u.loginName=?");
query.setString(0, name);
@SuppressWarnings("rawtypes")
List list=query.list();
User user=(User)list.get(0);
return user;
}
return null;
}
        public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}




HTTP Status 500 - No Session found for current thread

type Exception report

message No Session found for current thread

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.hibernate.HibernateException: No Session found for current thread
org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041)
user.hibernate.UserDao.getUser(UserDao.java:64)
global.struts.LoginAction.execute(LoginAction.java:134)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)




求大侠解决,怎么回事!
------解决思路----------------------
sessionFactory没有注入成功。
------解决思路----------------------
在web.xml里加入这个~~ hibernate4 才需要!

<!-- openSessionInView配置 -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
  相关解决方案