Java 当使用Spring/JPA并使用data.sql插入初始数据时,为什么ID列不是自动递增的?
错误: 实体:Java 当使用Spring/JPA并使用data.sql插入初始数据时,为什么ID列不是自动递增的?,java,spring,Java,Spring,错误: 实体: Exception in thread "task-2" org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of class path resource [data.sql]: INSERT INTO kana_term (id, en_term_text, jp_term_text
Exception in thread "task-2" org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of class path resource [data.sql]: INSERT INTO kana_term (id, en_term_text, jp_term_text) VALUES (DEFAULT, 'a', 'あ'); nested exception is org.postgresql.util.PSQLException: ERROR: null value in column "id" violates not-null constraint
Detail: Failing row contains (null, a, あ).
data.sql:
package io.blainelafreniere.langsite.entities;
import javax.persistence.*;
@Entity
public class KanaTerm {
@Id @GeneratedValue(strategy= GenerationType.AUTO) private Long id;
@Column String jp_term_text;
@Column String en_term_text;
}
我还尝试排除“(id)值(默认值)”内容:
INSERT INTO kana_term (id, en_term_text, jp_term_text) VALUES (DEFAULT, 'a', 'あ');
我得到了同样的错误
我认为我的实体已配置为不需要指定ID。我通过将
@GeneratedValue(strategy=GenerationType.AUTO)
更改为@GeneratedValue(strategy=GenerationType.IDENTITY)
我通过更改@GeneratedValue(strategy=GenerationType.AUTO)解决了我的问题
到@GeneratedValue(strategy=GenerationType.IDENTITY)
您是否可以共享您的表创建语句以确定。您使用的是什么数据库?你能用身份吗?@DanielJacob我在用PostgreSQL@Silverfang这是创建表输出:create table kana_term(id int8 not null,en_term_text varchar(255),jp_term_text varchar(255),primary key(id))
您可以将其修改为create table kana_term(id int8 not null AUTO_INCREMENT,en_term_text varchar(255),jp_term_text varchar(255),primary key(id))
您能否共享您的表创建语句以确定。您使用的是什么数据库?你能用身份吗?@DanielJacob我在用PostgreSQL@Silverfang这是创建表输出:create table kana_term(id int8 not null,en_term_text varchar(255),jp_term_text varchar(255),primary key(id))
您可以将其修改为create table kana_term(id int8 not null AUTO_INCREMENT,en_term_text varchar(255),jp_term_text varchar(255),主键(id))
INSERT INTO kana_term (en_term_text, jp_term_text) VALUES ('a', 'あ');