簡體   English   中英

自動增量不適用於PostgreSQL-Java EE APP

[英]Autoincrement does not work PostgreSQL - Java EE APP

我有可以自動增加PK ID的應用程序。 我必須添加其他自動增量列,並使用liquibase使其起作用-liquibase為自動增量創建了序列。 當我從查詢工具進行插入時,或者當我未在實體中映射此字段並使持久自動增量工作時。 但是當我添加:

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

我收到一個難看的錯誤:

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

怎么了

編輯:

我的liquibase變更集添加了此列並使其自動遞增:

<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用於從1開始

我猜應該改變數據庫中的表

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

....

您必須禁用可插入和可更新的false

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM