关键'PRIMARY'的Hibernate复制条目'7090'

时间:2013-04-02 08:24:18

标签: java mysql hibernate

当应用程序尝试在db中插入记录行时,我收到以下错误。

SQL Error: 1062, SQLState: 23000
ERROR org.hibernate.util.JDBCExceptionReporter  - Duplicate entry '7089' for key      'PRIMARY'
ERROR org.hibernate.event.def.AbstractFlushingEventListener  - Could not synchronize   database state with session
Caused by: java.sql.BatchUpdateException: Duplicate entry '7090' for key 'PRIMARY'
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1269)
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:955)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)

定义是

@Id
@Column(name = "CST_CUSTOMER_ID_PK")
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator")
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

发生错误的部分是:

savedCustomer = customerDao.save(customer);
            mtmrsLogger.debug("saved customer id:" + savedCustomer.getId());
            /**
             * Updating Customer Trans Table
             */
            updateCustomerTransForMakerChecker(customer,     customerform.getAuditDetails());

            /**
             * Updating Customer Audit
             */
            updateCustomerAuditForMakerChecker(customer, customerform.getAuditDetails());
            //status=1;
            //Add customer ewallet account
            updateCustomerInWalletBalance(customer, customerform.getAuditDetails());
            //send sms to customer
            smsManager.sendSMSToCUCustomer(customer.getMobileno(), userBean);
        }
        mtmrsLogger.exiting("CustomerManagerImpl", "addCustomer");

我的日志显示,程序已达到'退出类:CustomerManagerImpl方法:addCustomer'这一部分。我正在保存客户,我在其他两个表中设置相同的条目。客户表的主键是其他两个表中的外键。我失去了请帮助。

CREATE TABLE `CST_CUSTOMER_INFO` (
`CST_CUSTOMER_ID_PK` bigint(11) NOT NULL AUTO_INCREMENT,
`CST_MOBILE` varchar(30) DEFAULT NULL,
   `CST_FIRST_NAME` varchar(50) DEFAULT NULL,
`CST_LAST_NAME` varchar(150) NOT NULL,
`CST_MIDDLE_NAME` varchar(50) DEFAULT NULL,
PRIMARY KEY (`CST_CUSTOMER_ID_PK`)
) ENGINE=InnoDB AUTO_INCREMENT=4103 DEFAULT CHARSET=latin1

我偶尔会在制作中遇到错误,但在本地就可以了..

2 个答案:

答案 0 :(得分:2)

由于表格被多个应用程序修改,使用@GenericGenerator(name = "generator", strategy = "increment")会导致含糊不清。

<强>解释

  

策略增量:它生成long,short或类型的标识符   int只有在没有其他进程插入数据时才是唯一的   同桌。它不应该在集群环境中使用。

所以你应该重新考虑用什么策略来生成id。使用sequencenative策略可以解决您的问题。

答案 1 :(得分:-1)

替换

`CST_CUSTOMER_ID_PK` bigint(11) NOT NULL AUTO_INCREMENT,

`CST_CUSTOMER_ID_PK` bigint(11) NOT NULL,

您正在使用hibernate / java递增id,不需要让DBMS也增加它。