簡體   English   中英

休眠postgres id在10秒內遞增

[英]hibernate postgres id incrementing in 10s

我在使用休眠與Postgresql時遇到問題

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@Column(name = "id")
private Long id;

問題是id在10s內生成,例如

10
20
30

我該怎么做

1
2
3

正如已經指出的那樣,您應該使用另一個注釋@ javax.persistence.SequenceGenerator這是我的工作方式

@Id
@javax.persistence.SequenceGenerator(name = "order_id_sequence", sequenceName = "order_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "order_id_sequence")
@Column(name = "id")

請注意,由於數據庫無法在以下情況下回滾序列,因此您將無法依賴序列中沒有間隔的值:

  1. 會話A從序列中提取一個數字,例如1。
  2. 會話B從序列中提取數字2。
  3. 會話A回滾其事務。
  4. 會話B提交。

因此,我建議您不要擔心,也可以使用其他方法來產生一系列沒有間隙的唯一數字。

暫無
暫無

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

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