SSH 配置自动提交事务

来源:互联网 发布:招聘美工要求 编辑:程序博客网 时间:2024/06/10 23:33

今天在配置ssh的时候,在用到工具生成的save()方法时,相应的数据竟然没有保存到数据库。。原因是因为我的事务,没有配置好,按照网上的各种方法试了一遍,最后还是没有解决。于是我决定采用事务自动配置。

applicationcontext.xml.中改为

<tx:annotation-driven transaction-manager="transactionManager"/>   <bean id="sessionFactory"        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">        <property name="configLocation"            value="classpath:hibernate.cfg.xml">        </property>        <property name="hibernateProperties">        <props>         <prop key="hibernate.connection.autocommit">true</prop>          <prop key="hibernate.show_sql">true</prop>       </props>       </property>        </bean>

其他的配置没有改变,然后再dao实现类上用 @Transactional声明这里写图片描述

需要注意的是,原本自动生成的dao 用的是
getsession.save();
这里需要改成:sessionfactory.getCurrentsession()方法。这里写图片描述
自此问题全部解决。
还有一个方法是用hibernate的template类,就可以不用关心事务的配置。

0 0
原创粉丝点击