在mysql中插入数据时出现重复的主键错误

时间:2015-04-16 09:08:51

标签: java mysql jpa

我使用JPA在mysql数据库中插入数据,但是我收到了这个错误!

Query: InsertObjectQuery(eu.telecombretagne.loraFabian.KPI.KPI@7a363633)
javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'
Error Code: 1062
Call: INSERT INTO ReceivedPacketsStats (id, CodeRate, DataRate, Frequency, Signalisation, Lsnr, NodeMacAddress, MessageID, Modulation, Nature, Rssi, Size, Status, Time, timestamp, TypeMessage) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    bind => [16 parameters bound]
Query: InsertObjectQuery(eu.telecombretagne.loraFabian.KPI.KPI@7a363633)
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:157)
    at eu.telecombretagne.loraFabian.KPI.KPI.persist(KPI.java:138)
    at eu.telecombretagne.loraFabian.KPI.KPI.<init>(KPI.java:126)
    at eu.telecombretagne.loraFabian.nodeK.PacketFormatConverter.handleStats(PacketFormatConverter.java:275)
    at eu.telecombretagne.loraFabian.nodeK.PacketFormatConverter.NodeKPipePacket2RawData(PacketFormatConverter.java:77)
    at eu.telecombretagne.loraFabian.LoraNodeKConnector.receive(LoraNodeKConnector.java:108)
    at eu.telecombretagne.loraFabian.nodeK.NodeKPipe.receive(NodeKPipe.java:111)
    at eu.telecombretagne.loraFabian.nodeK.tunnel.TunnelPipe.receive(TunnelPipe.java:103)
    at eu.telecombretagne.loraFabian.nodeK.tunnel.UpstreamTunnel.filter(UpstreamTunnel.java:82)
    at eu.telecombretagne.loraFabian.nodeK.tunnel.AbstractTunnel$Filterer.work(AbstractTunnel.java:263)
    at eu.telecombretagne.loraFabian.nodeK.tunnel.AbstractTunnel$Worker.run(AbstractTunnel.java:237)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'
Error Code: 1062
Call: INSERT INTO ReceivedPacketsStats (id, CodeRate, DataRate, Frequency, Signalisation, Lsnr, NodeMacAddress, MessageID, Modulation, Nature, Rssi, Size, Status, Time, timestamp, TypeMessage) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    bind => [16 parameters bound]
Query: InsertObjectQuery(eu.telecombretagne.loraFabian.KPI.KPI@7a363633)
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:331)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:900)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:962)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:631)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:558)
    at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:2002)
    at org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:298)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.insertObject(DatasourceCallQueryMechanism.java:377)
    at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:165)
    at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:180)
    at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:489)
    at org.eclipse.persistence.queries.InsertObjectQuery.executeCommit(InsertObjectQuery.java:80)
    at org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(InsertObjectQuery.java:90)
    at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:301)
    at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:58)
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)
    at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:798)
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:108)
    at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:85)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1804)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1786)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1737)
    at org.eclipse.persistence.internal.sessions.CommitManager.commitNewObjectsForClassWithChangeSet(CommitManager.java:226)
    at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:125)
    at org.eclipse.persistence.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:4207)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1441)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1531)
    at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:277)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1169)
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:132)
    ... 10 more
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3256)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1313)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1585)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1500)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1485)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:890)
    ... 41 more
11:03:16.339 [UpstreamTunnel-Filterer-[0]] WARN  eu.telecombretagne.loraFabian.nodeK.tunnel.AbstractTunnel - Exception in UpstreamTunnel-Filterer-[0]
java.lang.IllegalStateException: 
Exception Description: No transaction is currently active
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.rollback(EntityTransactionImpl.java:176) ~[eclipselink.jar:2.5.2.v20140319-9ad6abd]
    at eu.telecombretagne.loraFabian.KPI.KPI.persist(KPI.java:141) ~[classes/:?]
    at eu.telecombretagne.loraFabian.KPI.KPI.<init>(KPI.java:126) ~[classes/:?]
    at eu.telecombretagne.loraFabian.nodeK.PacketFormatConverter.handleStats(PacketFormatConverter.java:275) ~[classes/:?]
    at eu.telecombretagne.loraFabian.nodeK.PacketFormatConverter.NodeKPipePacket2RawData(PacketFormatConverter.java:77) ~[classes/:?]
    at eu.telecombretagne.loraFabian.LoraNodeKConnector.receive(LoraNodeKConnector.java:108) ~[classes/:?]
    at eu.telecombretagne.loraFabian.nodeK.NodeKPipe.receive(NodeKPipe.java:111) ~[classes/:?]
    at eu.telecombretagne.loraFabian.nodeK.tunnel.TunnelPipe.receive(TunnelPipe.java:103) ~[classes/:?]
    at eu.telecombretagne.loraFabian.nodeK.tunnel.UpstreamTunnel.filter(UpstreamTunnel.java:82) ~[classes/:?]
    at eu.telecombretagne.loraFabian.nodeK.tunnel.AbstractTunnel$Filterer.work(AbstractTunnel.java:263) ~[classes/:?]
    at eu.telecombretagne.loraFabian.nodeK.tunnel.AbstractTunnel$Worker.run(AbstractTunnel.java:237) [classes/:?]

帮我解决一下,谢谢!

3 个答案:

答案 0 :(得分:1)

  • 对象ReceivedPacketsStats传递id为1。
  • 但是在Database ReceivedPacketsStats表中已经记录了id为1的文件。
  • 这就是它抛出MySQLIntegrityConstraintViolationException的原因:重复条目&#39; 1&#39;。

答案 1 :(得分:0)

谢谢大家,

我通过删除phpmyadmin中的索引ID来解决问题!

答案 2 :(得分:-1)

您正在明确传递要插入的记录的ID。

可能您传递的是已存在的ID。使用数据库中的数据进行检查。