当前位置: 代码迷 >> Java Web开发 >> ssh session没有绑定到线程的异常
  详细解决方案

ssh session没有绑定到线程的异常

热度:2187   发布时间:2016-04-10 22:50:52.0
ssh session没有绑定到线程的错误
废话不多说, 直接贴配置:


spring 事务配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>


<!-- 事务配置 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>

</bean>


<!-- 配置事务特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<!-- 配置哪些方法将要被切入 -->
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>




<aop:config proxy-target-class="true">
<!-- 配置切入点:指哪些包下的类将要被切面所切入 第一个星号:任意返回值  第二个星号:dao包下面的子子包 第三个星号:任意类-->
<aop:pointcut id="allmethod" expression="execution(* dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allmethod" />
</aop:config>



spring dao层配置:
<bean id="userDao" class="dao.UserDAO">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>

</bean>



web.xml配置:
<!-- spring配置 -->	
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>


<!-- spring与structs 整合的配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 配置spring控制session的开关 -->
  <filter>  
  <filter-name>OpenSessionInViewFilter</filter-name>   
  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>   
   <!--  
    <init-param>  
       <param-name>sessionFactory</param-name>  
       <param-value>classpath:hibernate.cfg.xml</param-value>
      </init-param>
     --> 
  </filter>  
  
  <filter-mapping>  
  <filter-name>OpenSessionInViewFilter</filter-name>   
  <url-pattern>/*</url-pattern>   
  </filter-mapping>  

<!-- structs2配置 -->
<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>





测试代码(我就直接在dao里面写了个main方法测试):




package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import bean.User;


public class UserDAO extends HibernateDaoSupport{

//根据id取对象
public  User find(int user_id){
Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
User user = (User)session.get(User.class,user_id);
return user;

}



public static void main(String[] d){
ApplicationContext ac =   
            new ClassPathXmlApplicationContext("applicationContext-dao.xml");  
        UserDAO dao =(UserDAO) ac.getBean("userDao"); 
      

User user = dao.find(81);
System.out.println(user);
}
}







最后是报错信息:

Exception in thread "main" org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
  相关解决方案