成功解决InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMod

偶然遇到一个报错: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.
    at org.springframework.orm.hibernate5.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1047)
    at org.springframework.orm.hibernate5.HibernateTemplate.lambda$save$11(HibernateTemplate.java:614)
    at org.springframework.orm.hibernate5.HibernateTemplate.doExecute(HibernateTemplate.java:367)
    at org.springframework.orm.hibernate5.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:334)
    at org.springframework.orm.hibernate5.HibernateTemplate.save(HibernateTemplate.java:613)

看了下源码发现是 HibernateTemplate.save方法在内部会自己创建session,并且设置session.FlushMode为FlushMode.MANUAL,代表只读权限,无法写入,困扰了我好几天,最后发现是事务的问题,配置了自定义事务后就好了。

你可能感兴趣的:(java,java)