org.springframework.dao.InvalidDataAccessApiUsageException

org.springframework.dao.InvalidDataAccessApiUsag eException:
Write operations are not allowed in read-only mode ( FlushMode.NEVER/MANUAL):  Turn  your  Session into  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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值