当我在DB中保存时,我尝试自动生成不是PK的值。我创建了具有值的实体:
class Entity {
// Other values
@NaturalId
@SequenceGenerator(name = "number_sequence", sequenceName =
"number_sequence")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "number_sequence")
private Long number;
}以及序列的脚本:
CREATE SEQUENCE schema.number_sequence AS BIGINT
INCREMENT 1
START 1
OWNED BY table_name.number;但是,当我在没有number的情况下构建实体并将其保存到DB时,我会遇到一个错误:
org.postgresql.util.PSQLException: ERROR: null value in column "number" violates not-null
constraint
Detail: Failing row contains (e925b4fb-5147-4754-b949-08d79a6ad764, 2020-06-04
14:31:50.49584+03, null, bd765ef29c3211e98b6b019787d6f1ee,
1e100b1da97b11e98b6b511f0c71b787).我哪里错了?
发布于 2020-06-04 21:12:48
感谢卡亚曼和SternK。我做了什么:
@NaturalId
@Generated(value = GenerationTime.INSERT)
@Column(insertable = false, updatable = false)
private Long number;在我的实体上:
CREATE SEQUENCE number_sequence AS BIGINT
INCREMENT 1
START 1
OWNED BY table.number;
ALTER TABLE table
ALTER COLUMN number SET DEFAULT nextval('number_sequence');在我的实体里。如果上面的查询不起作用,可以添加schema.number_seq或schema.table
https://stackoverflow.com/questions/62193756
复制相似问题