mysql – 键’PRIMARY’的重复条目’…’

前端之家收集整理的这篇文章主要介绍了mysql – 键’PRIMARY’的重复条目’…’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

正如你和我都知道的那样,关于这种错误信息有很多问题.

但我找不到任何好的答案,因为答案太多了.

我有一个存储客户端发送的nonces的表.

但有时(偶尔)db会抱怨重复的主键插入,即使没有完全相同主键的记录也是如此.

这是JVM显示内容.

  1. [#|2012-11-09T11:06:52.098+0900|WARNING|glassfish3.1.2|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=236;_ThreadName=Thread-2;|EJB5184:A system exception occurred during an invocation on EJB Nonce2Bean,method: public java.lang.Object kr.co.ticomms.gameground.business.AbstractEntityFacade.persist(java.lang.Object)|#]
  2. [#|2012-11-09T11:06:52.099+0900|WARNING|glassfish3.1.2|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=236;_ThreadName=Thread-2;|javax.ejb.EJBException
  3. ...
  4. Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
  5. Internal Exception: com.MysqL.jdbc.exceptions.jdbc4.MysqLIntegrityConstraintViolationException: Duplicate entry 'c8b4bdb84606fed0-c8b4bdb84606fed0_1352426820765_1880007534138556' for key 'PRIMARY'
  6. Error Code: 1062
  7. Call: INSERT INTO NONCE2 (NONCE,UDID,CREATED_DATE) VALUES (?,?,?)
  8. bind => [3 parameters bound]
  9. Query: InsertObjectQuery(c8b4bdb84606fed0/c8b4bdb84606fed0_1352426820765_1880007534138556402)
  10. ...
  11. ... 29 more
  12. Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
  13. Internal Exception: com.MysqL.jdbc.exceptions.jdbc4.MysqLIntegrityConstraintViolationException: Duplicate entry 'c8b4bdb84606fed0-c8b4bdb84606fed0_1352426820765_1880007534138556' for key 'PRIMARY'
  14. Error Code: 1062
  15. Call: INSERT INTO NONCE2 (NONCE,?)
  16. bind => [3 parameters bound]
  17. Query: InsertObjectQuery(c8b4bdb84606fed0/c8b4bdb84606fed0_1352426820765_1880007534138556402)
  18. ...
  19. ... 59 more
  20. Caused by: com.MysqL.jdbc.exceptions.jdbc4.MysqLIntegrityConstraintViolationException: Duplicate entry 'c8b4bdb84606fed0-c8b4bdb84606fed0_1352426820765_1880007534138556' for key 'PRIMARY'
  21. |#]

这是MysqL显示内容.

  1. MysqL> SHOW VARIABLES LIKE "%version%";
  2. +-------------------------+-------------------------+
  3. | Variable_name | Value |
  4. +-------------------------+-------------------------+
  5. | protocol_version | 10 |
  6. | version | 5.1.62-0ubuntu0.10.04.1 |
  7. | version_comment | (Ubuntu) |
  8. | version_compile_machine | x86_64 |
  9. | version_compile_os | debian-linux-gnu |
  10. +-------------------------+-------------------------+
  11. 5 rows in set (0.00 sec)
  12. MysqL> DESC NONCE2;
  13. +--------------+--------------+------+-----+-------------------+-------+
  14. | Field | Type | Null | Key | Default | Extra |
  15. +--------------+--------------+------+-----+-------------------+-------+
  16. | CREATED_DATE | timestamp | NO | | CURRENT_TIMESTAMP | |
  17. | UDID | varchar(255) | NO | PRI | NULL | |
  18. | NONCE | varchar(255) | NO | PRI | NULL | |
  19. +--------------+--------------+------+-----+-------------------+-------+
  20. 3 rows in set (0.00 sec)
  21. MysqL> SHOW CREATE TABLE NONCE2;
  22. +--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  23. | Table | Create Table |
  24. +--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  25. | NONCE2 | CREATE TABLE `NONCE2` (
  26. `CREATED_DATE` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,`UDID` varchar(255) NOT NULL,`NONCE` varchar(255) NOT NULL,PRIMARY KEY (`UDID`,`NONCE`)
  27. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
  28. +--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  29. 1 row in set (0.00 sec)
  30. +--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
  31. | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
  32. +--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
  33. | NONCE2 | 0 | PRIMARY | 1 | UDID | A | 7 | NULL | NULL | | BTREE | |
  34. | NONCE2 | 0 | PRIMARY | 2 | NONCE | A | 403 | NULL | NULL | | BTREE | |
  35. +--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
  36. 2 rows in set (0.00 sec)
  37. MysqL>

我尝试删除并重新创建表,但会出现同样的问题.

我在GlassFish上使用JPA.

有帮助吗?

—————————————–更新

我正在使用JPA.

ID类.

  1. public class NonceId implements Serializable {
  2. public static NonceId newInstance(final String udid,final String nonce) {
  3. if (udid == null) {
  4. throw new IllegalArgumentException("null udid");
  5. }
  6. if (nonce == null) {
  7. throw new IllegalArgumentException("null nonce");
  8. }
  9. final NonceId instance = new NonceId();
  10. instance.udid = udid;
  11. instance.nonce = nonce;
  12. return instance;
  13. }
  14. @Override
  15. public int hashCode() {
  16. int hash = 7;
  17. hash = 23 * hash + (this.udid != null ? this.udid.hashCode() : 0);
  18. hash = 23 * hash + (this.nonce != null ? this.nonce.hashCode() : 0);
  19. return hash;
  20. }
  21. @Override
  22. public boolean equals(Object obj) {
  23. if (obj == null) {
  24. return false;
  25. }
  26. if (getClass() != obj.getClass()) {
  27. return false;
  28. }
  29. final NonceId other = (NonceId) obj;
  30. if ((this.udid == null) ? (other.udid != null) : !this.udid.equals(other.udid)) {
  31. return false;
  32. }
  33. if ((this.nonce == null) ? (other.nonce != null) : !this.nonce.equals(other.nonce)) {
  34. return false;
  35. }
  36. return true;
  37. }
  38. @Override
  39. public String toString() {
  40. return udid + "/" + nonce;
  41. }
  42. private String udid;
  43. private String nonce;
  44. }

实体类.

  1. @Entity
  2. @IdClass(NonceId.class)
  3. @Table(name = "NONCE2")
  4. @XmlTransient
  5. public class Nonce2 implements Serializable {
  6. public static final int UDID_SIZE_MIN = 1;
  7. public static final int UDID_SIZE_MAX = 255;
  8. public static final int NONCE_SIZE_MIN = 1;
  9. public static final int NONCE_SIZE_MAX = 255;
  10. public static Nonce2 newInstance(final String udid,final String nonce) {
  11. if (nonce == null) {
  12. throw new NullPointerException("null value");
  13. }
  14. final Nonce2 instance = new Nonce2();
  15. instance.udid = udid;
  16. instance.nonce = nonce;
  17. return instance;
  18. }
  19. public Date getCreatedDate() {
  20. return createdDate;
  21. }
  22. public String getUdid() {
  23. return udid;
  24. }
  25. public String getNonce() {
  26. return nonce;
  27. }
  28. @PrePersist
  29. protected void _PrePersist() {
  30. createdDate = new Date();
  31. }
  32. @Override
  33. public String toString() {
  34. return udid + "/" + nonce;
  35. }
  36. @Column(name = "CREATED_DATE",nullable = false,updatable = false)
  37. @Temporal(TemporalType.TIMESTAMP)
  38. @NotNull
  39. private Date createdDate;
  40. @Id
  41. @Column(name = "UDID",updatable = false)
  42. @NotNull
  43. @Size(min = UDID_SIZE_MIN,max = UDID_SIZE_MAX)
  44. private String udid;
  45. @Id
  46. @Column(name = "NONCE",updatable = false)
  47. @NotNull
  48. @Size(min = NONCE_SIZE_MIN,max = NONCE_SIZE_MAX)
  49. private String nonce;
  50. }

在我的过滤器中我做到了

  1. @WebFilter(urlPatterns = {"/*"})
  2. public class Filter_ implements Filter {
  3. @Override
  4. public void doFilter(final ServletRequest request,final ServletResponse response,final FilterChain chain)
  5. throws IOException,ServletException {
  6. // check whether nonce is already exist via em.find();
  7. chain.doFilter(request,response);
  8. // store nonce via em.persist(); // EXCEPTION IS HERE
  9. // THERE IS NO SUCH RECORD check direct sql console.
  10. }
  11. }

我的JPA提供程序似乎执行此语句.

  1. INSERT INTO NONCE2 (NONCE,?)
最佳答案
您在表上定义了复合主键.

可能是您尝试在同一会话期间批量插入记录.并且批处理可能包含所述键列的重复条目.请检查一下.

另外,请发布带有示例数据的插入代码.

猜你在找的MySQL相关文章