当我输入用户名和密码提交后报错!
这句没通过 List list=studentDaoImpl.getStuByName(name);org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean$TransactionAwareInvocationHandler.invoke(AbstractSessionFactoryBean.java:300)
at $Proxy0.getCurrentSession(Unknown Source)
我有事务声明:
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.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/demo"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>ssh/model/StudentInfo.hbm.xml</value></list>
</property>
</bean>
<bean id="studentDaoImpl1" class="ssh.dao.impl.StudentDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean name="/login" class="ssh.struts.action.LoginAction" >
    <property name="studentDaoImpl">
        <ref local="studentDaoImpl1"/>
    </property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
             <property name="sessionFactory">
             <ref local="sessionFactory"/>
             </property> 
        </bean> 
        <bean id="studentDaoImpl" 
              class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
              <property name="transactionManager">
                  <ref bean="transactionManager"/>
              </property>
              <property name="target">
                  <ref bean="studentDaoImpl1"/>
              </property>
              <property name="transactionAttributes">
                  <props>
                    <prop key="get*">PROPAGATION_REQUIRED</prop>
                  </props>
              </property>
        </bean>谢谢大家!!

解决方案 »

  1.   

    建议找个SSH教程仔细配一遍。
    看你的代码应该是用spring管理struts的action
      

  2.   

    看你的配置没有什么问题,看一下你的getStuByName()方法
      

  3.   

    有没有可能是sessionFactory生成的问题 ?
    我有点不明白,我在applicationContext.xml声明的是
    <bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    而在我的DAO实现类里面注入的是
    import org.hibernate.SessionFactory;
    这两个产生的sessionFactory是同一个吗?
      

  4.   

    <bean id="studentDaoImpl1" class="ssh.dao.impl.StudentDaoImpl"> 
    <property name="sessionFactory"> 
    <ref local="sessionFactory"/> 
    </property> 
    你这样用 你的实现类就应该是:
    public class studentDaoImpl1 implements xxxDAO extends hibernateDaoSupport{
         实现xxxDAO中的方法
          在方法中需要使用数据库,使用Hibernatetemplate来访问,他的作用和Hibernate中的Session类似
    }
    Spring和hibernate整合 使用Spring提供的hibernateDaoSupport类 在XML中注入SessionFactory
      

  5.   

    我的书上讲到:
    spring和hibernate整合有两种方式实现DAO
    一种是继承HibernateDaoSupport
    一种是基于Hibernate3.* 实现Dao
    第一种直接由spring提供,由spring管理session ,不用事务
    第二种是hibernate自己管理session,由spring注入SessionFactory
    同时spring为hibernate提供事务管理 。
    第一种我试了 可以实现
    现在我想试第二种方式,就是想学一下spring里面事务的使用
    现在使用这个方式的时候出错了。
    我上面出现了不一样的SessionFactory,我还是不明白该怎么解决