使用的框架是ssh.
今天在使用hibernate的HibernateTemplate模板进行插入操作事报错了,错误如下:
Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition
意思是只读模式下写的操作是不允许的。
这是因为在spring中readonly这个属性默认是true,即默认是只读模式,所以我们要修改它的为false。
有两种方法:第一种是直接使用注解的方式,直接在DAO层的实现类上加上如下注释即可(在方法上添加也可以):
@Transactional(readOnly = false)
第二种是在配置文件里面配置readyonly为false
!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--事务的策略-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--在这里配置read-only="false"-->
<tx:method name="*" propagation="REQUIRED" read-only="false" />
</tx:attributes>
</tx:advice>
<!--事务的aop-->
<aop:config>
<aop:pointcut expression="execution(* com.mysql.dao.*.* (..))"
id="mypointcut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut" />
</aop:config>