当我们单独使用hibernate时,我们都会记得通过session.getTransaction().commit()来提交事务。
可是当我们使用Spring和Hibernate整合时,而且是使用HibernateTemplate时,就无法手动提交事务。这时就会出现:sql正常输出,可是数据插不进数据库的问题。
解决:
1、我们可以配置Hibernate的自动提交事务,在hibernate.cfg.xml中,在<session-factory>下,加上<property name="connection.autocommit">true</property>。
2、如果关于Hibernate和数据库的相关配置是在Spring的配置文件中,则可以:在下面相应的地方配上autocommit
<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.SQLServerDialect
</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
3、我们也可以不使用hibernateTemplate,而是使用sesseionFactory/session,来操纵数据。