使用hibernate 5 取序號異常狀況,DB為oracle,ID生成annotation設定如下
public class OrderDet implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="OrderDetSeq")
@SequenceGenerator(name="OrderDetSeq", sequenceName="ORDERDET_SEQ")
@Column(name="ID")
private BigInteger id;
public BigInteger getId() {
return id;
}
public void setId(BigInteger id) {
this.id = id;
}
}
例如:DB SEQ為100,預期取出為101 (SEQ.nextval),但實際為60
這個問題在hibernate 4沒遇到,查了一下主要是以下原因
生成器演算法的調整(V4 to V5):
(old) hi/lo: it uses the hi/lo algorithm and it’s equivalent to the original seqhilo generator.
(new) pooled: This optimizer uses a hi/lo optimization strategy, but the current in-memory identifiers highest boundary is extracted from an actual database sequence value.
hibernate 5會產生一個生成池供程式使用,避免多次去讀取DB,當池內數值用完,再去DB取一段SEQ回來(DB.SEQ會向後推一段空間),