在spring整合hibernate是初学者会遇到这样一个问题:
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
这是这是因为没有配置事务管理器:
在beans.xml文件,即spring 配置文件中对事务管理器进行配置如下:
<!--1.配置事务管理器--> <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!--2.开启事务注解--> <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
同时在Action调用的service层类名上加上注解:@Transactional