首页 > 解决方案 > hibernate_sequence 创建重复键 'PRIMARY'

问题描述

以前工作的 Spring-Boot 应用程序出现了一个错误。它最后一次工作 100%。我对代码进行了零更改。似乎在 hibernate_sequence 表上输入了重复的主键。

今天和我的导师开发人员一起工作了三个小时。我们都难住了。我们尝试使用不同的数据库、重命名并启动应用程序的备份。尝试了不同的方法在实体上生成 id。我们将 Spring-Boot 更新为最新版本。每次我们删除/删除 hibernate_sequence 表时,您可以在控制台中看到它是在初始应用程序启动时生成的,您会得到Hibernate: insert into hibernate_sequence values (1)两次。在这一点上,由于代码没有改变并且上周三运行良好,我的导师觉得这可能是我们不知道的地方的更新?

工作代码的 Github Repo:https ://github.com/chrisyoung0101/DrinkWithWineApp

IMG 1:生成 hibernate_sequence 之前的数据库/IMG 2:应用程序启动时的控制台/IMG 3:生成 hibernate_sequence 之前的数据库

尝试保存到 MySQL 中的配对表后出现错误:

2019-05-19 18:33:23.698  WARN 4405 --- [nio-8080-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1062, SQLState: 23000
2019-05-19 18:33:23.698 ERROR 4405 --- [nio-8080-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper   : Duplicate entry '1' for key 'PRIMARY'
2019-05-19 18:33:23.702 ERROR 4405 --- [nio-8080-exec-7] o.h.i.ExceptionMapperStandardImpl        : HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
2019-05-19 18:33:23.717 ERROR 4405 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [PRIMARY]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'PRIMARY'

MySQL BEFORE hibernate_sequence 表生成 控制台显示键 'PRIMARY' 的重复条目 MySQL AFTER hibernate_sequence 表生成

标签: javahibernatespring-boot

解决方案


推荐阅读