解決辦法有兩種:
1. <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true </prop>
<prop key="hibernate.connection.autocommit">true </prop>
</props>
</property>
<property name="configLocation" value="/WEB-INF/hibernate.cfg.xml">
</property>
</bean>
將<prop key="hibernate.connection.autocommit">true</prop>設定為true即可解決,實現事務自動認可
2.使用Spring的交易管理
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:config>
<aop:pointcut expression="execution(public * service.*.*(..))"
id="bussinessService" />
<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="exists" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>