HSQLDB批量删除失败但在Oracle中它可以正常工作

时间:2014-07-04 14:28:12

标签: sql oracle hibernate hsqldb

我有一个设置:Spring + Hibernate + JPA + Junit来测试我的代码。

我有Oracle作为数据库。当我测试我的代码时,每个测试都通过了这个代码甚至在生产中。我想在内存中使用HSQLDB来代替Oracle.Most测试仍然通过HSQLDB传递,但是一个特定的测试总是因HQSQLDB而失败。 (在Oracle中工作)。

这里引起麻烦的行。 (我使用log4jdbc输出sql语句)。

为两个陈述创建批处理:

  

1:从IMAGE_TAGS中删除IMAGE_TAGS_KEY = 1

     

2:从IMAGE_TAGS中删除,其中IMAGE_TAGS_KEY = 2

在JPA中,我正在寻找实体来删除它们。在第一次迭代时工作正常,但是当我到达第二项时,它会因此异常而失败:

org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
    at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:85)
    at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
    at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
    at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
    at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2666)
    at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2911)
    at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:189)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:64)
    at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1185)
    at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1283)
    at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:117)
    at com.videotron.imagemanager.dao.ImageContentGroupDAOImpl.removeImageContentGroup(ImageContentGroupDAOImpl.java:143)
    at com.videotron.imagemanager.dao.ImageContentGroupDAOImpl$$FastClassByCGLIB$$8ef3e206.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:627)
    at com.videotron.imagemanager.dao.ImageContentGroupDAOImpl$$EnhancerByCGLIB$$a153b522.removeImageContentGroup(<generated>)
    at com.videotron.imagemanager.service.ImageManagerServiceImpl.cleanContent(ImageManagerServiceImpl.java:270)
    at com.videotron.imagemanager.service.ImageManagerServiceImpl$$FastClassByCGLIB$$82a03251.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:627)
    at com.videotron.imagemanager.service.ImageManagerServiceImpl$$EnhancerByCGLIB$$1a5a3f0d.cleanContent(<generated>)
    at com.videotron.imagemanager.image.impl.v1.ImageManagerImpl.cleanContent(ImageManagerImpl.java:1454)
    at com.videotron.imagemanager.image.impl.v1.ImageManagerImpl$$FastClassByCGLIB$$4ded3863.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
    at com.videotron.imagemanager.image.impl.v1.ImageManagerImpl$$EnhancerByCGLIB$$c6ce4df.cleanContent(<generated>)
    at com.videotron.imagemanager.service.ImageManagerServiceFacade.cleanContent(ImageManagerServiceFacade.java:350)
    at com.videotron.imagemanager.service.ImageManagerServiceFacade$$FastClassByCGLIB$$558f388b.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:627)
    at com.videotron.imagemanager.service.ImageManagerServiceFacade$$EnhancerByCGLIB$$fb734807.cleanContent(<generated>)
    at com.videotron.imagemanager.service.ImageManagerServiceFacadeTest.testCleanContent(ImageManagerServiceFacadeTest.java:671)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

1 个答案:

答案 0 :(得分:0)

异常org.hibernate.StaleStateException的原因主要是因为

  

“Hibernate缓存会话中的对象。如果对象是   修改过,而Hibernate不知道它,它会抛出这个   异常”。

  

在提交对象之前刷新数据可能会导致全部清除   对象等待持久化。

我建议将spring和log4jdbc的日志级别设置为debug并启用hibernate的show_sql这个willl会给你清晰的想法。

更新

首先看我认为:

On第一次迭代当你说删除IMAGE_TAGS_KEY 1和2时,hibernate会触发此查询,这样你就可以通过log4jdbc记录这个查询,但仍然没有提交事务,所以更改仍然没有写入DB和第二次再次迭代你正在删除待反映的相同键。