Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

     但今天自己在写删除数据的方法时跑出如下异常,

org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
	org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:686)
	org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
	org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
	org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
	org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:837)
	org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:833)
	com.hainnu.dao.impl.ProductDaoImpl.delete(ProductDaoImpl.java:31)
	com.hainnu.service.impl.ProductServiceImpl.delete(ProductServiceImpl.java:34)
	com.hainnu.action.ProductAction.delete(ProductAction.java:33)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	java.lang.reflect.Method.invoke(Unknown Source)

解决的方法就是:我在删除的方法要调用this.getHibernateTemplate().de;ete(xxxxxx). 这里的xxxx代表的是对象。

所以我们要想的这个对象,我们就要用id去找到这个对象。

             错误方法:Product product = this.getHibernateTemplate().load(Product.class, id);

            正确写法:Product product = this.getHibernateTemplate().get(Product.class, id);

 到网上也查找了相关关于load和get 方法的区别:

                   1. 

 这一点也正要印证了我们这个错误:load()方法在缓存中找到数据。别人把数据库中的数据修改了,load如何在缓存中找到了数据,则不会再访问数据库,而get则
会返回最新数据。

                   2.

getHibernateTemplate.load() 存在延迟加载问题。
          getHibernateTemplate.get()  不存在此问题,她是不采用lazy机制的。
          当记录不存在时候,get方法返回null,load方法产生异常,即get()可以取空的数据集,但load()不行。
        3.
load方法可以返回实体的代理类,get方法则返回真是的实体类
        4 load方法可以充分利用hibernate的内部缓存和二级缓存中的现有数据,而get方法仅仅在内部缓存中
进行数据查找,如果没有发现数据則将越过二级缓存,直接调用SQL查询数据库。


  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值