工作中使用到了oracle12的版本,使用hibernate5保存時發現生成的結果除了1正常其餘的都從-49開始往上加,
數據庫自身查詢是沒有問題的。
跟蹤hibernate5的源碼發現@SequenceGenerator還需要配置一個allocationSize=1才能正常
如
@SequenceGenerator(name="UMS_USER_SEQ", sequenceName="UMS_USER_SEQ",allocationSize = 1)
相關源碼在org.hibernate.id.enhanced.PooledOptimizer#generate
generationState.value = callback.getNextValue();
// unfortunately not really safe to normalize this
// to 1 as an initial value like we do the others
// because we would not be able to control this if
// we are using a sequence...
if ( generationState.value.lt( 1 ) ) {
log.pooledOptimizerReportedInitialValue( generationState.value );
}
// the call to obtain next-value just gave us the initialValue
if ( ( initialValue == -1
&& generationState.value.lt( incrementSize ) )
|| generationState.value.eq( initialValue ) ) {
generationState.hiValue = callback.getNextValue();
}
else {
generationState.hiValue = generationState.value;
generationState.value = generationState.hiValue.copy().subtract( incrementSize - 1 );
}