您的位置:首页 > 编程语言 > Java开发

org.springframework.dao.InvalidDataAccessApiUsageException

2014-07-14 13:53 459 查看
org.springframework.dao.InvalidDataAccessApiUsageException:

Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Sessioninto FlushMode.COMMIT/AUTO or
remove 'readOnly' marker from transaction definition.

这个问题搞了大半天最后搞出这么个结论当然是可以解决我的问题了!可是具体是怎么回事我说不大明白,目前只知道这样做就可以了。在OA项目中要删除和保存的时候总会出现上面的错误,这里主要原因是OpenSessionInViewFilter有一个变量flushMode,可通过getter setter为其赋值,而其默认值为FlushMode.NEVER,所以在使用getHibernateTemplate()的时候直接就出错

public void delOrg(int orgId) {

Orgnization org = (Orgnization)this.getHibernateTemplate().load(Orgnization.class, new Integer(orgId));

if(org.getChild().size()>0){

throw new RuntimeException("存在子机构不能删除!");

}

this.getHibernateTemplate().delete(org);

}

于是我就使用古老的方式,这样就没有问题,当然是按照网上的同仁们提供的session.setFlushMode(FlushMode.AUTO);添加进去就可以,当然不加同样是上面的错误。

public void delOrg(int orgId) {

Session session = this.getSessionFactory().getCurrentSession();

session.setFlushMode(FlushMode.AUTO);

session.beginTransaction();

Orgnization org = (Orgnization)session.load(Orgnization.class, new Integer(orgId));

if(org.getChild().size()>0){

throw new RuntimeException("存在子机构不能删除!");

}

session.delete(org);

session.flush();

session.beginTransaction().commit();

}

于是我又想这样的话getHibernateTemplate().也是可以拿到getCurrentSession于是我就把原来使用getHibernateTemplate().改为

public void delOrg(int orgId) {

this.getHibernateTemplate().getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);

System.out.println(getSessionFactory().getCurrentSession()

.getFlushMode());;

Orgnization org = (Orgnization)this.getHibernateTemplate().load(Orgnization.class, new Integer(orgId));

if(org.getChild().size()>0){

throw new RuntimeException("存在子机构不能删除!");

}

this.getHibernateTemplate().delete(org);

}

结果发现也是可以的,其实也很好理解。在这过程中又发现this.getHibernateTemplate().setFlushMode(2);这个也是可以的,试了1、0、2,发现2就可以!呵呵!具体的我没查!应该有地方会说明的!

public void delOrg(int orgId) {

this.getHibernateTemplate().setFlushMode(2);

System.out.println(this.getHibernateTemplate().getFlushMode());

System.out.println(getSessionFactory().getCurrentSession()

.getFlushMode());;

Orgnization org = (Orgnization)this.getHibernateTemplate().load(Orgnization.class, new Integer(orgId));

if(org.getChild().size()>0){

throw new RuntimeException("存在子机构不能删除!");

}

this.getHibernateTemplate().delete(org);

}

最终本人解决办法

dao层

public Piccomment add(Piccomment piccomment) {

getHibernateTemplate().setFlushMode(2);

return super.add(piccomment);

}

web.xml

<!-- 延迟加载用到的过滤器 -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<!-- 解决:延迟加载 -->
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
<!-- <init-param>
<param-name>singleSession</param-name>
<param-value>false</param-value>
</init-param>
-->
</filter>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐