我正在尝试创建一个将执行 CRUD 操作的 Spring Boot 应用程序。但是当我启动应用程序时,它会因异常而终止。我不知道我做错了什么。这是我的 DDL 和 DML。我不知道
schema.sql
CREATE TABLE IF NOT EXISTS Person(
first_name VARCHAR(20),
last_name VARCHAR(20),
person_id varchar(10),
PRIMARY KEY(person_id));
CREATE INDEX IF NOT EXISTS person_id_index on Person(person_id);
CREATE INDEX IF NOT EXISTS person_last_name_index on Person(last_name);
data.sql
INSERT INTO Person(person_id,first_name,last_name) VALUES('ABCDE12345','Jane','Doe');
错误日志
018-02-03 23:12:33.916 INFO 88786 --- [ main] com.springboot.rest.PersonDaoTest : Started PersonDaoTest in 336.053 seconds (JVM running for 336.71)
2018-02-03 23:12:34.023 INFO 88786 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from class path resource [schema.sql]
2018-02-03 23:12:34.023 INFO 88786 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from class path resource [schema.sql] in 0 ms.
2018-02-03 23:12:34.023 INFO 88786 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from class path resource [data.sql]
2018-02-03 23:12:34.037 WARN 88786 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@5c33f1a9] to process 'before' execution of test method [public void com.springboot.rest.PersonDaoTest.testCreatePerson() throws java.lang.Exception] for test instance [com.springboot.rest.PersonDaoTest@577f9dfd]
org.springframework.jdbc.datasource.init.ScriptStatementFailedException:
Failed to execute SQL script statement #1 of class path resource [data.sql]:
INSERT INTO Person(person_id,first_name,last_name) VALUES('ABCDE12345','Jane','Doe');
nested exception is org.h2.jdbc.JdbcSQLException:
Unique index or primary key violation: "PRIMARY_KEY_8 ON
PUBLIC.PERSON(PERSON_ID) VALUES ('ABCDE12345', 1)"; SQL statement:
INSERT INTO Person(person_id,first_name,last_name) VALUES('ABCDE12345','Jane','Doe') [23505-196]
at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:491) ~[spring-jdbc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:238) ~[spring-jdbc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:48) ~[spring-jdbc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
. . . .
. . . .
. . . .
. . . .
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207) [.cp/:na]
Caused by: org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "PRIMARY_KEY_8 ON PUBLIC.PERSON(PERSON_ID) VALUES ('ABCDE12345', 1)"; SQL statement:
INSERT INTO Person(person_id,first_name,last_name) VALUES('ABCDE12345','Jane','Doe') [23505-196]
application.properties
server.contextPath=/rest
spring.h2.console.enabled=true
spring.h2.console.path=/rest/h2
spring.datasource.url=jdbc:h2:file:./rest
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
最佳答案
我想您的应用程序在启动时尝试使用 person_id
= ABCDE12345
将记录添加到数据库,并且您会收到异常,因为此类记录已经存在。
PRIMARY KEY(person_id)
添加了一个约束,确保 person_id
列中仅存在唯一值。
有了这个限制,每次使用非唯一的 record_id
将记录添加到数据库时,您都会收到唯一索引或主键冲突
错误。
关于java - 尝试在 Spring boot 应用程序中将行插入 H2 数据库时出现主键冲突异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48605862/