简体   繁体   中英

Autoincrement does not work PostgreSQL - Java EE APP

I have application in which I have autoincrement for PK Id. I had to add other autoincremented column and I used liquibase to make it work - liquibase created sequence for auto increment. When I make insert from query tool or when i do not map this field in entity and make persist autoincrement works. But when I add :

@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "next_value", nullable=false, unique=true)
private Long nextValue;

I get an ugly error:

Caused by: org.postgresql.util.PSQLException: ERROR: empty value in the "next_value" column violates the limit of the required value

What is wrong here?

EDIT:

My liquibase changeset to add this column and make it autoincrement:

<addColumn tableName="table">
      <column name="next_value" type="number"/>
    </addColumn>
<addAutoIncrement
        columnDataType="number"
        columnName="next_value"
        incrementBy="1"
        startWith="1"
        tableName="table"/>
<sql>select setval('table_next_value_seq', (select cast(current_value+1 as bigint) from gapless_sequence), false)</sql>

setval was used to start it not from 1

i guess it should change table in db

ALTER TABLE tblName(
nextValue LONG NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),

....

You have to disable insertable and updatable false

@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "next_value", unique = true, nullable = false, insertable = false,updatable = false)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM