The following exception occurred today while practicing how to perform unit tests in SSH:
Org.springframework.dao.DataIntegrityViolationException:Could not execute JDBC batch update; Nested exception is org.hibernate.exception.ConstraintViolationException:Could not execute JDBC batch Updateat Org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException ( sessionfactoryutils.java:636) at Org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException ( hibernatetransactionmanager.java:789) at Org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit (hibernatetransactionmanager.java:663) at Org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit ( abstractplatformtransactionmanager.java:732) at Org.springframework.transaction.support.AbstractPlatformTransactionManager.commit ( abstractplatformtransactionmanager.java:701) at Org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning ( transactionaspectsupport.java:321) at Org.springframework.transAction.interceptor.TransactionInterceptor.invoke (transactioninterceptor.java:116) at Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (reflectivemethodinvocation.java:171) at Org.springframework.aop.framework.JdkDynamicAopProxy.invoke (jdkdynamicaopproxy.java:204) at com.sun.proxy.$ Proxy8.save (Unknown Source) at Junit.test.UserServiceTest.testSave (userservicetest.java:28) at SUN.REFLECT.NATIVEMETHODACCESSORIMPL.INVOKE0 (Native Method) at Sun.reflect.NativeMethodAccessorImpl.invoke ( Unknown source) at Sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown source) at Java.lang.reflect.Method.invoke (Unknown Source) 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.runners.ParentRunNer.runleaf (parentrunner.java:271) at Org.junit.runners.BlockJUnit4ClassRunner.runChild ( BLOCKJUNIT4CLASSRUNNER.JAVA:70) at Org.junit.runners.BlockJUnit4ClassRunner.runChild (Blockjunit4classrunner.java : @ 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.junit.internal.runners.statements.RunBefores.evaluate (runbefores.java:26) at Org.junit.runners.ParentRunner.run (parentrunner.java:309) 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) caused by:org.hibernate.exception.ConstraintViolationException:Could not execute JDBC batch Updateat Org.hibernate.exception.SQLStateConverter.convert (sqlstateconverter.java:71) at Org.hibernate.exception.JDBCExceptionHelper.convert (jdbcexceptionhelper.java:43) at Org.hibernate.jdbc.AbstractBatcher.executeBatch (abstractbatcher.java:249) at Org.hibernate.engine.ActionQueue.executeActions (actionqueue.java:235) at Org.hibernate.engine.ActionQueue.executeActions (actionqueue.java:139) at Org.hibernate.event.def.AbstractFlushingEventListener.performExecutions (abstractflushingeventlistener.java:298 ) at Org.hibernate.event.def.DefaultFlushEventListener.onFlush (defaultflusheventlistener.java:27) at Org.hibernate.impl.SessionImpl.flush (sessionimpl.java:1000) at Org.hibernate.impl.SessionImpl.manaGedflush (sessionimpl.java:338) at Org.hibernate.transaction.JDBCTransaction.commit (jdbctransaction.java:106) at Org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit (hibernatetransactionmanager.java:655) ... morecaused by:java.sql.BatchUpdateException:Duplicate entry ' felayman1 ' for key 1at Com.mysql.jdbc.PreparedStatement.executeBatchSerially (preparedstatement.java:2054) at Com.mysql.jdbc.PreparedStatement.executeBatch (preparedstatement.java:1467) at Com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch (newproxypreparedstatement.java:1723) at Org.hibernate.jdbc.BatchingBatcher.doExecuteBatch (batchingbatcher.java:48) at Org.hibernate.jdbc.AbstractBatcher.executeBatch (abstractbatcher.java:242) ... morecaused by:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Duplicate Entry ' Felayman1 ' for key 1at Sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) at Sun.reflect.NativeConstructorAccessorImpl.newInstance (Unknown SOurce) at Sun.reflect.DelegatingConstructorAccessorImpl.newInstance (Unknown Source) at Java.lang.reflect.Constructor.newInstance (Unknown Source) at Com.mysql.jdbc.Util.handleNewInstance (util.java:411) At Com.mysql.jdbc.Util.getInstance (util.java:386) at Com.mysql.jdbc.SQLError.createSQLException (Sqlerror.java : 1041) at Com.mysql.jdbc.MysqlIO.checkErrorPacket (mysqlio.java:4187) at Com.mysql.jdbc.MysqlIO.checkErrorPacket ( mysqlio.java:4119) at Com.mysql.jdbc.MysqlIO.sendCommand (mysqlio.java:2570) at Com.mysql.jdbc.MysqlIO.sqlQueryDirect (mysqlio.java:2731) at Com.mysql.jdbc.ConnectionImpl.execSQL ( connectionimpl.java:2815) at Com.mysql.jdbc.PreparedStatement.executeInternal (preparedstatement.java:2155) at Com.mysql.jdbc.PreparedStatement.executeUpdate (preparedstatement.java:2458) at Com.mysql.jdbc.PreparedStatement.executeBatchSerially (preparedstatement.java:2006) ... More
I looked at the stackable exception information and learned several key information that caused the problem:JDBC batch update,constraintviolationexception,Duplicate entry,key
This means that we have violated the SQL syntax constraint and repeatedly defined the primary key, that is, I accidentally inserted two primary keys of the same record, the database certainly did not.
Core Issues:Could not execute JDBC batch update, nested exception is org.hibernate.exception.ConstraintViolationException : Could not execute JDBC batch update
cause of cause :
Org.hibernate.exception.ConstraintViolationException:Could not execute JDBC batch update
Java.sql.BatchUpdateException:Duplicate entry ' Felayman1 ' for key 1
Com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Duplicate entry ' Felayman1 ' for key 1
Workaround, modify the primary key of the data information you want to insert, and do not duplicate the primary key in the table corresponding to the database.