org.hibernate.StaleStateExceptionorg.hibernate.StaleStateException: Batchupdatereturnedunexpectedrowcountfromupdate [0]; actualrowcount: 0; expected: 1atorg.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:67) atorg.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:54) atorg.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:46) atorg.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3071) atorg.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2950) atorg.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3330) atorg.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:145) atorg.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:560) atorg.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:434) atorg.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337) atorg.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39) atorg.hibernate.internal.SessionImpl.flush(SessionImpl.java:1282) atorg.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:465) atorg.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2963) atorg.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2339) atorg.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485) atorg.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147) atorg.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38) atorg.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231) atorg.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65) attop.simba.dao.HibernateDao.fn02(HibernateDao.java:73) atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod) atsun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource) atsun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource) atjava.lang.reflect.Method.invoke(UnknownSource) atorg.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) atorg.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) atorg.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) atorg.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) atorg.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) atorg.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) atorg.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) atorg.junit.runners.ParentRunner$3.run(ParentRunner.java:290) atorg.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) atorg.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) atorg.junit.runners.ParentRunner.access$000(ParentRunner.java:58) atorg.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) atorg.junit.runners.ParentRunner.run(ParentRunner.java:363) atorg.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) atorg.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
解决方案:
有些数据库是可以修改自增主键例如:mysql;
有些数据库是不允许修改自增主键的例如postgresql;
不要设置自增主键的值