Duplicate entry ' 1 ' for key ' PRIMARY ' (Error)

Source: Internet
Author: User

When using Mysql+ibatis for insertion testing, error: Information is as follows:

Test Insert Com.ibatis.common.jdbc.exception.NestedSQLException:---The error occurred in com/study/ibatis/Student.xml.---The error occurred whileapplying a parameter map. ---Check the addstudent-Inlineparametermap. ---Check The statement (update failed). ---Cause:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Duplicate entry ' 1 ' forKey ' PRIMARY 'At com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate (Mappedstatement.java:107) at Com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert (Sqlmapexecutordelegate.java:393) at Com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert (Sqlmapsessionimpl.java:82) at Com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.insert (Sqlmapclientimpl.java:58) at Com.study.ibatis.StudentDaoImpl.addStudent (Studentdaoimpl.java:33) at Com.study.ibatis.TestIbatis.main (Testibatis.java:14) caused By:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Duplicate entry' 1 ' forKey ' PRIMARY 'At Sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) at sun.reflect.NativeConstructorAcces Sorimpl.newinstance (Nativeconstructoraccessorimpl.java:39) at Sun.reflect.DelegatingConstructorAccessorImpl.newInstance (Delegatingconstructoraccessorimpl.java:27) at Java.lang.reflect.Constructor.newInstance (Constructor.java:513) at Com.mysql.jdbc.Util.handleNewInstance (Util.java:406) at Com.mysql.jdbc.Util.getInstance (Util.java:381) at Com.mysql.jdbc.SQLError.createSQLException (Sqlerror.java:1015) at Com.mysql.jdbc.SQLError.createSQLException (Sqlerror.java:956) at Com.mysql.jdbc.MysqlIO.checkErrorPacket (Mysqlio.java:3491) at Com.mysql.jdbc.MysqlIO.checkErrorPacket (Mysqlio.java:3423) at Com.mysql.jdbc.MysqlIO.sendCommand (Mysqlio.java:1936) at Com.mysql.jdbc.MysqlIO.sqlQueryDirect (Mysqlio.java:2060) at Com.mysql.jdbc.ConnectionImpl.execSQL (Connectionimpl.java:2542) at Com.mysql.jdbc.PreparedStatement.executeInternal (Preparedstatement.java:1734) at Com.mysql.jdbc.PreparedStatement.execute (Preparedstatement.java:995) at Com.ibatis.sqlmap.engine.execution.SqlExecutor.executeUpdate (Sqlexecutor.java:80) at Com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteUpdate (Mappedstatement.java:216) at Com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate (Mappedstatement.java:94)    ... 5 Morefalse

Error means: Repeat input "1" key for "main" into the database, query table tbl_student, found three records, one of the primary key is 1. Given that I was testing, the default primary key in the database was 1. So delete the record with the primary key 1 in the database. Then the test was passed.

Summary: The primary key in Ibatis is set to the default of 1. Causes the test to start directly from the default of 1. Then an error is made, and setting the primary key default value to null resolves the problem.

Duplicate entry ' 1 ' for key ' PRIMARY ' (Error)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.