Struts Problem Report
Struts has detected an unhandled exception:
Messages:
Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
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
File: org/hibernate/jdbc/Expectations.java
Line number: 85
Stacktraces
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.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793)
org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:664)
org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:392)
对于错误Batch update returned unexpected row count from update [0];对应的中文意思就是:批量更新从更新[0]返回未预期的行计数;简而言之的意思就是,找不到可以更新的那一行,就是找不到相应的id值。
我在写javaweb中的saveOrUpdate的时候就遇到了这个问题,通过不断的调式,才明白了哪有问题。修改更新带id参数传进去是没问题的。但是你新增的时候,会连带隐藏的id(这时候id=“”而不是null)传进去,而后台hibernate看到id不等于null,便以为是更新,但找不到对应的id进行数据更新,而且系统也没有做相应的报错功能,所以直接变成以上错误。下来来讲解两个我实现过的解决方法。
在更新表格数据的时候,双击列表,我们需要传参id,新增保存的时候,我们不需要传参id,(ps:id是自增的uuid)。然而为了偷懒,更新修改和新建保存都用的是同一个表格的时候,这才会出问题。
第一种方法:重复写表格,第一个更新修改的表格,form里带隐含参数,
第二个表格就把<input type="hidden" ......>删掉即可,对应的URL跳转改好就可以了。
第二种方法:修改action类,在调用业务层的时候做数据判断,对id的值进行一定的修改,若是id="",即把他改为id=null。