今天在配置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类,就可以不用关心事务的配置。