Java 如何在postgresql中设置不同于id的自动增量字段
我在postgresql表中有id列和position列。我希望位置自动递增,因此我使用串行数据类型添加了位置:Java 如何在postgresql中设置不同于id的自动增量字段,java,postgresql,auto-increment,Java,Postgresql,Auto Increment,我在postgresql表中有id列和position列。我希望位置自动递增,因此我使用串行数据类型添加了位置: 更改表tbl添加列位置序列 问题是,当我尝试创建新对象时,出现以下错误: org.postgresql.util.PSQLException:error:PSQLException:error:position列中的null值与notnull值冲突 如何设置位置列自动递增 我的普通对象: public class Stg { @Id @GeneratedValue
更改表tbl添加列位置序列代码>
问题是,当我尝试创建新对象时,出现以下错误:
org.postgresql.util.PSQLException:error:PSQLException:error:position列中的null值与notnull值冲突
如何设置位置列自动递增
我的普通对象:
public class Stg {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
@NotNull
@Size(max = 20)
@Column(name = "name", length = 20, nullable = false)
private String name;
private Integer position;
首先,您需要创建序列:
CREATE SEQUENCE sys_sequence_name START -9223372036854775808 MINVALUE -9223372036854775808 MAXVALUE 9223372036854775807;
然后创建表,但在声明行参数add之后
CREATE TABLE table_name (
id bigserial primary key,
var_with_seq bigint not null default nextval('sys_sequence_name')
nextval('sys\U sequence\U name')-这将使用您创建的序列。您的Postgres版本是什么?@TimBiegeleisen 10.1您的INSERT语句是什么?您的alter语句本身不应该导致此错误。@404我在语句中输入的值如下所示(5,S5,null)所以位置显然是空的,但是我没有id的问题-它是生成的,我应该在Stg类的位置字段中添加一些注释吗?您可以在上面添加私有整数位置@SequenceGenerator(name=“sys\u sequence\u name”)和ofc add@GeneratedValue(strategy=GenerationType.sequence,generator=“sys\u sequence\u name”)我以前尝试过使用此注释,但不起作用。postgres中的序列运行良好,但java仍然尝试将null放入位置HMM,尝试添加注释@SequenceGenerator(name=“sys_seq_position_name”,sequenceName=“sys_Sequence_name”,allocationSize=1)@GeneratedValue(strategy=GenerationType.Sequence,generator=“sys_Sequence_position_name”)@列(name=“position”,insertable=false)私人多头头寸
CREATE TABLE table_name (
id bigserial primary key,
var_with_seq bigint not null default nextval('sys_sequence_name')