好奇怪啊·~为什么我用spring+hibernate
修改数据库信息,数据库里的信息没变
但是却能查询到修改后的数据?只有重新载入服务器才能知道还是原来的数据 spring管理hibernate,不是可以自动管理事务
提交等等吗~~我在视频上看的 只要继承了hibernateDaoSupport
是不是我在spring 配置文件里少配置了什么东西  所以事务没有提交 ??

解决方案 »

  1.   

    你没配置事务管理吧,HibernateTransactionManager配了么
      

  2.   

    继承了hibernateDaoSupport  需要配置事务管理吗?
      

  3.   

    spring文件我只配置了这个
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
    <property name="configLocation">
    <value>WEB-INF/hibernate.cfg.xml</value>
    </property>
    </bean>
    是不是要配置事务管理啊·~这是我的dao代码
    public class StudentDao  extends HibernateDaoSupport implements IStudentDao {
    //添加学生
     public void inputStu(Student student){
     
     HibernateTemplate ht=this.getHibernateTemplate(); 
       try {
       
       
       
    ht.save(student);

    ht.flush();
    } catch (Exception e) {

    e.printStackTrace();}


    }
      

  4.   

    public class StudentDao  extends HibernateDaoSupport implements IStudentDao { 
    //添加学生 
    public void inputStu(Student student){ HibernateTemplate ht=this.getHibernateTemplate(); 
      try { 
      
      
      
    ht.save(student); 
    ht.connection().commit();
    ht.flush(); 
    } catch (Exception e) { e.printStackTrace();} 
    }
      

  5.   

    可以先这样,手动改写某个DAO在其中手动开启事务,手动提交事务,测试该方法,如果通过那就是事务的问题了。如果想利用Spring来管理事务的话,可以去网上找示例代码研究下,对于事务的配置还是比较简单的。
    参考:
    <!-- 配置sessionFactory -->
    <bean id="sessionFactory"               class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
    <value>classpath:hibernate.cfg.xml</value>
    </property>
    </bean>           

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

    <!-- 配置事务的传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <!-- 注册事务 -->
    <tx:method name="save" propagation="REQUIRED"/>
    <tx:method name="delete" propagation="REQUIRED"/>
    <tx:method name="update" propagation="REQUIRED"/>
    <tx:method name="*" read-only="true"/>
    </tx:attributes>
    </tx:advice><!-- 哪些类的哪些方法参与事务 -->
    <aop:config>
    <aop:pointcut id="allDaoMethod" expression="execution(* org.wzc.exam.dao.*.*(..))"/>
    <aop:advisor pointcut-ref="allDaoMethod" advice-ref="txAdvice"/>
    </aop:config>
    希望有所帮助……
      

  6.   

    继上,对于相应的DAO需要实现对应的接口,否则可能会抛代理异常……
      

  7.   

    我的资源里有本spring参考书,用过的朋友评价不错 
    另外有本myeclipse教程书,对jdk安装、tomcat部署、struts、spring、hibernate配置、整合介绍的尤其详细 
    有需要的朋友可以去看看
      

  8.   

    可能你在创建sessionFactory时还是用Hibernate创建的,如果交给Spring管理的话,就不必让Hibernate创建sessenFactory了。而让spring来创建.
      

  9.   

    没有 我是全都交给spring管理的·~~~
      

  10.   

    HibernateTemplate 在dao里 到底应该怎么用呢·~
    通过 HibernateDaosupport 能不能管理事务?
      

  11.   

    我现在想知道 ~~~
    HibernateTemplate 在dao里 到底应该怎么用呢·~ 
    通过 HibernateDaosupport 能不能管理事务? 
      

  12.   

    那就要看你是要把Spring的事物控制在哪一层了,一般是在service层,即业务层。