在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
本文介绍了解决Spring整合Hibernate时出现的“Writeoperationsarenotallowedinread-onlymode”错误的方法。通过配置事务管理器并开启事务注解,确保在需要写操作的服务层正确使用事务。
364

被折叠的 条评论
为什么被折叠?



