1.如果在数据层调用HibernateTemplate来实现数据的CRUD,会报如下错误:
HTTP Status 500 - Request processing failed; nested exception is 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.
方1(不可行):在web.xml文件中配置如下:
方2:(可行)[有前提]
在数据层通过SessionFactory得到Session对象来实现CRUD,
引发的问题:update方法和 delete方法不起作用,控制台也不报错也不打印sql语句
解决方法:自己用SQL语句写一下,不要直接调用session的update、delete方法,举个例子:
1//this.getSession().delete(user);
2this.getSession().createQuery("delete from User where id=?").setParameter(0, id).executeUpdate();
把1句该为2句就对了
原因:不太清楚,可能是Hibernate优化的原因吧,那天有时间看看源码,再来补充
小小问题:浪费我如此长的时间,希望能帮助其他小伙伴。
HTTP Status 500 - Request processing failed; nested exception is 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.
方1(不可行):在web.xml文件中配置如下:
<filter>这是网上大部分的解决方法,通过查看源码能看到对于Hibernate3可能是适用的,但是对于Hibernate4(我用的是4.0.8)是不适用的
<filter-name>openSessionInViewerFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<!-- <init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param> -->
</filter>
<filter-mapping>
<filter-name>openSessionInViewerFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
方2:(可行)[有前提]
在数据层通过SessionFactory得到Session对象来实现CRUD,
引发的问题:update方法和 delete方法不起作用,控制台也不报错也不打印sql语句
解决方法:自己用SQL语句写一下,不要直接调用session的update、delete方法,举个例子:
1//this.getSession().delete(user);
2this.getSession().createQuery("delete from User where id=?").setParameter(0, id).executeUpdate();
把1句该为2句就对了
原因:不太清楚,可能是Hibernate优化的原因吧,那天有时间看看源码,再来补充
小小问题:浪费我如此长的时间,希望能帮助其他小伙伴。