gpt4 book ai didi

java - 带有 postgresql 的串行列上的 Spring Data JPA "null value in column xxx violates not-null constraint"

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:55:38 25 4
gpt4 key购买 nike

我的实体有一个 mapOrder 字段,我希望它像下面这样自动递增:

@Entity
public class Map{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(columnDefinition = "serial")
private Long mapOrder;

//.......
}

生成的 sql 看起来不错:

CREATE TABLE map
(
id bigserial NOT NULL,
map_order serial NOT NULL,
...
)

但是当我用 Spring Data JPA 的存储库保存它时,像这样:

Map m=new Map();
repo.save(m);

会给我异常(exception):

Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "map_order" violates not-null constraint

有什么想法吗?

最佳答案

尝试将您的代码更改为:

@GeneratedValue(strategy = GenerationType.SEQUENCE)

引用:https://stackoverflow.com/a/29028369

关于java - 带有 postgresql 的串行列上的 Spring Data JPA "null value in column xxx violates not-null constraint",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30690098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com