org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException

  1. 云栖社区>
  2. 博客>
  3. 正文

org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException

黄威的世界 2016-05-11 13:36:27 浏览4096
展开阅读全文

项目是Java Web

使用框架:struts,hibernatespring

构建工具:maven

使用一个商品管理系统,用于管理超市商品

 

项目结构如下:

 执行如下操作时报错

 点击“delete”,删除超市时,报错。

注意:是有时候报错,不是每次删除都报错。

报错信息如下:

Xml代码  收藏代码
  1. Struts Problem Report  
  2.   
  3. Struts has detected an unhandled exception:  
  4. Messages:     
  5.   
  6.     Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1  
  7.     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  
  8.   
  9. File:   org/hibernate/jdbc/Expectations.java  
  10. Line number:    81  
  11. Stacktraces  
  12. org.springframework.orm.hibernate4.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  
  13.   
  14.     org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:181)  
  15.     org.springframework.orm.hibernate4.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:680)  
  16.     org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:562)  
  17.     org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)  
  18.     org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)  
  19.     org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475)  
  20.     org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270)  
  21.     org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)  
  22.     org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)  
  23.     org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)  
  24.     org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)  
  25.     org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)  
  26.     com.shop.jn.dao.SupermarketDao$$EnhancerByCGLIB$$2c3435ff.delete()  
  27.     com.shop.jn.action.supermarket.DeleteSupermarketAction.execute(DeleteSupermarketAction.java:20)  
  28.     sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  29.     sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)  
  30.     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)  
  31.     java.lang.reflect.Method.invoke(Method.java:597)  
  32.     com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)  
  33.     com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)  
  34.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)  
  35.     com.shop.jn.interceptor.LoggerInterceptor.intercept(LoggerInterceptor.java:23)  
  36.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  37.     org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)  
  38.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  39.     com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)  
  40.     com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)  
  41.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  42.     com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)  
  43.     org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)  
  44.     com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)  
  45.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  46.     com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)  
  47.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  48.     com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)  
  49.     com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)  
  50.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  51.     com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)  
  52.     com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)  
  53.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  54.     com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)  
  55.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  56.     org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)  
  57.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  58.     org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)  
  59.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  60.     org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)  
  61.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  62.     com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)  
  63.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  64.     com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)  
  65.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  66.     com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)  
  67.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  68.     com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)  
  69.     com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)  
  70.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  71.     com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)  
  72.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  73.     org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)  
  74.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  75.     com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)  
  76.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  77.     com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)  
  78.     com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)  
  79.     org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)  
  80.     org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511)  
  81.     org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)  
  82.     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)  
  83.     org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)  
  84.     org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)  
  85.     org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)  
  86.     org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)  
  87.     org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)  
  88.     org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)  
  89.     org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)  
  90.     org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)  
  91.     org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)  
  92.     org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)  
  93.     org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)  
  94.     java.lang.Thread.run(Thread.java:662)  
  95.       
  96.   
  97. org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1  

 

原因分析

执行删除超市的action 是DeleteSupermarketAction,内容如下:

Java代码  收藏代码
  1. package com.shop.jn.action.supermarket;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4. import com.shop.jn.dao.SupermarketDao;  
  5. import com.shop.jn.entity.Supermarket;  
  6. /*** 
  7.  * 删除超市. 
  8.  *  
  9.  * @author huangwei 
  10.  * 
  11.  */  
  12. public class DeleteSupermarketAction extends ActionSupport {  
  13.   
  14.     private static final long serialVersionUID = 7753616847784594121L;  
  15.     private Supermarket supermarket;  
  16.     private SupermarketDao supermarketDao;  
  17.   
  18.     @Override  
  19.     public String execute() throws Exception {  
  20.         this.supermarketDao.delete(this.supermarket);  
  21.         return super.execute();  
  22.     }  
  23.   
  24.     public Supermarket getSupermarket() {  
  25.         return supermarket;  
  26.     }  
  27.   
  28.     public void setSupermarket(Supermarket supermarket) {  
  29.         this.supermarket = supermarket;  
  30.     }  
  31.   
  32.     public SupermarketDao getSupermarketDao() {  
  33.         return supermarketDao;  
  34.     }  
  35.   
  36.     public void setSupermarketDao(SupermarketDao supermarketDao) {  
  37.         this.supermarketDao = supermarketDao;  
  38.     }  
  39.       
  40. }  

 说明:supermarket 是通过依赖注入的,注入的字段是id(http://localhost:8080/shop_goods/supermarket/deleteSupermarket.action?supermarket.id=9)。

此时supermarket 的状态是detached 脱管的(脱离管理的),this.supermarketDao.delete 的方法体如下:

Java代码  收藏代码
  1. this.sessionFactory.getCurrentSession().delete(obj);  

但是hibernate api中对delete 的方法说明如下:

void

delete(Object object) 
          Remove a persistent instance from the datastore.

 要求参数必须是persistent 持久化的的状态。

 

如何解决呢?

使用原生的sql语句

我在dao中新增了一个方法:

Java代码  收藏代码
  1. public void deleteHQL(int id) {  
  2.         this.sessionFactory.getCurrentSession()  
  3.                 .createQuery("delete from Supermarket where id=?")  
  4.                 .setInteger(0, id).executeUpdate();  
  5.     }  

 在DeleteSupermarketAction 中调用如下:

Java代码  收藏代码
  1. @Override  
  2.     public String execute() throws Exception {  
  3.         this.supermarketDao.deleteHQL(this.supermarket.getId());  
  4.         return super.execute();  
  5.     }  

 

网友评论

登录后评论
0/500
评论
黄威的世界
+ 关注