Spring Data JPA“第xxx列中的空值违反了非空约束".在PostgreSQL的串行列上 [英] Spring Data JPA "null value in column xxx violates not-null constraint" on serial column with postgresql

查看:134
本文介绍了Spring Data JPA“第xxx列中的空值违反了非空约束".在PostgreSQL的串行列上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My entity has a mapOrder field which I want auto-increment like below:

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

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

    //.......
}

生成的sql看起来不错:

The sql generated seems good:

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

但是当我将其保存在Spring Data JPA的存储库中时,就像这样:

But when I save it with Spring Data JPA's repository, like this:

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

会给我例外:

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

有什么想法吗?

推荐答案

尝试将您的代码更改为此:

Try changing your code to this:

@GeneratedValue(strategy = GenerationType.SEQUENCE)

参考: https://stackoverflow.com/a/29028369

这篇关于Spring Data JPA“第xxx列中的空值违反了非空约束".在PostgreSQL的串行列上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆