title: Why Hibernate SequenceStyleGenerator together with default value for column produces gaps in generated IDs?
body: <h2>The problem</h2>
  <p>I have a table with a numeric ID in Oracle database and I want to generate this ID using a sequence, so when I create a table I set <code>ID NUMERIC DEFAULT ON NULL TEST_SEQ.nextval</code>. I use Hibernate together with JPA and SpringBoot to manipulate this table in Java. I use Hibernate's SequenceStyleGenerator as a generator for the ID (see example below). When I save several entities I see that generated IDs have gaps. The sequence starts with 1 and it's incremented by 1 and when I save 5 entities generated IDs will be 1,3,5,7,9 instead of the expected 1,2,3,4,5.</p>
  <p>But when I set <code>ID NUMERIC DEFAULT TEST_SEQ.nextval</code> (&quot;on null&quot; is missing here compared to the first case) id generation works as expected. <strong>So the question is why in the first case ids are generated with gap and in the second case they are generated as you would expect?</strong> I assume it's due to some interaction between Hibernate and that default value, but I have a limited understanding of Hibernate and I can't understand what's exactly happening.</p>
  <h2>Example:</h2>
  <p>I create a sequence and table like this.</p>
  <pre><code>CREATE SEQUENCE TEST_SEQ
      START WITH 1
      INCREMENT BY 1
      NOCACHE
      NOCYCLE;
  
  CREATE TABLE TABLE_A
  (
      ID         NUMBER DEFAULT ON NULL TEST_SEQ.nextval,
      TEXT       VARCHAR2(200),
      RND_NUM    NUMBER,
      CREATED_AT DATE NOT NULL,
  
      CONSTRAINT PK_TABLE_F PRIMARY KEY (ID)
  );
  </code></pre>
  <p>I define the following entity</p>
  <pre><code>@Entity
  @Table(name = &quot;TABLE_A&quot;)
  public class EntityA {
      private static final String SEQUENCE_NAME = &quot;TEST_SEQ&quot;;
  
      @Id
      @Column(name = &quot;ID&quot;, nullable = false)
      @GenericGenerator(name = SEQUENCE_NAME, strategy = &quot;org.hibernate.id.enhanced.SequenceStyleGenerator&quot;,
          parameters = {
              @Parameter(name = &quot;sequence_name&quot;, value = SEQUENCE_NAME),
              @Parameter(name = &quot;increment_size&quot;, value = &quot;1&quot;)
          })
      @GeneratedValue(generator = SEQUENCE_NAME, strategy = GenerationType.SEQUENCE)
      private Long id;
      
      @Column(name = &quot;TEXT&quot;)
      private String text;
  
      @Column(name = &quot;RND_NUM&quot;)
      private Long randomNumber;
  
      @Column(name = &quot;CREATED_AT&quot;)
      @CreationTimestamp
      @Temporal(TemporalType.DATE)
      private Date createdAt;
    
  }
  </code></pre>
  <p>I have a simple JPA repository</p>
  <pre><code>public interface EntityARepository extends JpaRepository&lt;EntityF, Long&gt; {
  }
  </code></pre>
  <p>And to test this I have the following service</p>
  <pre><code>@Service
  public class AService {
      private final EntityARepository repository;
  
      public FService(EntityARepository repository) {
          this.repository = repository;
      }
  
      public void saveEntities(int numIter) {
          List&lt;Long&gt; generatedIds = new ArrayList&lt;&gt;();
          for (int i = 0; i &lt;= numIter; i++) {
              long rndNum = new Random().nextLong();
              String text = String.format(&quot;Random number is %d&quot;, rndNum);
              EntityA entity = new EntityA();
              entity.setRandomNumber(rndNum);
              entity.setText(text);
  
              EntityA savedEntity = repository.save(entity);
              generatedIds.add(savedEntity.getId());
          }
          System.out.println(&quot;Generated IDs: &quot; + generatedIds);
      }
  }
  </code></pre>
  <p>So when I call saveEntities with numIter=5 I get the following output</p>
  <pre><code>Generated IDs: [1,3,5,7,9]
  </code></pre>
  <p>After that, I modify the ID column to remove &quot;default on null&quot;</p>
  <pre><code>alter table TABLE_A modify ID default TEST_SEQ.nextval
  </code></pre>
  <p>And after I call saveEntities again I get:</p>
  <pre><code>Generated IDs: [11,12,13,14,15]
  </code></pre>
  <p>And as you can see there is no gaps between ids.</p>
  <h2>Dependencies</h2>
  <p>I use Hibernate 5.4 and Oracle 18c XE.</p>