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