首页 > 解决方案 > 插入数据库时​​如何修复主键的重复条目?

问题描述

我有一个 android 应用程序,用户在表中插入记录。该应用程序已经运行了几个月。在过去的几天里,我收到了主要错误的重复条目。我不确定问题是什么。

我正在为我的实体使用 GenerationType 序列。会不会是序列号关闭了?如果是这样,为什么会这样?我还从另一个应用程序添加到此表中,这可能是问题吗?

我试过改变记录插入的方式,但我仍然得到同样的错误。也许如果我知道为什么会突然发生这种情况,我就能找出代码。

@Entity(name="bmwsales")
@Table(name="bmwsales")
public class Bmwsales implements Serializable{
private static final long serialVersionUID = 1L;

@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.SEQUENCE)
private Integer id;

@Column(name="customerfirstname")
private String customerfirstname;

   ..................

服务实施地点:

@Override
public void addBmwvehicle(Bmwsales vehicle) {
    rolllogger.info("add bmw customer vehicle");
 bmwsalesRepository.save(vehicle);
}

错误:

Duplicate entry '35475' for key 'PRIMARY'
org.hibernate.engine.jdbc.spi.SqlExceptionHelper in logExceptions at line 131
org.hibernate.engine.jdbc.spi.SqlExceptionHelper in convert at line 110
org.hibernate.engine.jdbc.batch.internal.BatchingBatch in performExecution at line 128
org.hibernate.engine.jdbc.batch.internal.BatchingBatch in doExecuteBatch at line 104
org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl in execute at line 147
org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl in executeBatch at line 212
org.hibernate.engine.spi.ActionQueue in executeActions at line 629
org.hibernate.engine.spi.ActionQueue in executeActions at line 474

我希望根据需要插入记录。提前致谢!

标签: javaandroidspring-bootspring-data-jpa

解决方案


推荐阅读