title: Hibernate Migration 4.x to 5.0.7.Final: the framework uses hibernate_sequence instead of mySequence (with Oracle and H2)
body: <p>I have updated the hiberate version from 4.0.1.Final to 5.0.7.Final(porting from Jboss 7 to Wildfly 10).</p>
  
  <p><strong>Problem:</strong>
  The hibernate framwork uses hibernate_sequence for the id generation instead of mySequence.</p>
  
  <p><strong>Hibernate configuration:</strong>
  In order to maintain the backward compatibility, hibernate.id.new_generator_mappings is configured as false value.</p>
  
  <p><strong>Entity configuration:</strong>
  The id field is annotated with the following JPA API:</p>
  
  <pre><code>@Id
  @GeneratedValue(strategy = GenerationType.AUTO, generator = "myGenerator")
  @SequenceGenerator(name = "myGenerator", sequenceName = "MY_GENERATOR_SEQ")
  @Column(name="ID")
  private Long id;
  </code></pre>
  
  <p><strong>DB configuration:</strong>
  The MY_GENERATOR_SEQ sequence is defined with the "INCREMENT BY 1" option.</p>
  
  <p>The problem occurs for both H2 and Oracle DB.</p>
  
  <blockquote>
    <p><strong>QUESTION</strong>: <em>I have analyzed the implementation and I don't know if it could be a BUG of Hibernate or any possible bad configuration i did for the backward compatibility???</em></p>
  </blockquote>
  
  <hr>
  
  <p><strong>Analysis:</strong>
  Hibernate during server startup gets some initialization:
  SequenceStyleGenerator.configure method defines the QualifiedName sequenceName as "hibernate_sequence", because it tried to get the value from the Properties params.
  At this point I noticed that the MY_GENERATOR_SEQ sequenceName was present into the Properties params at runtime with the "sequence" key (sequence=MY_GENERATOR_SEQ) but the retrieve strategy (determineSequenceName) needs the "sequence_name" as key.
  So the key used to set the sequence name into the Properties params is from the deprecated class org.hibernate.id.SequenceGenerator.SEQUENCE because of hibernate.id.new_generator_mappings=false, 
  but the SequenceStyleGenerator class needs the org.hibernate.id.enhanced.SequenceStyleGenerator.SEQUENCE_PARAM key in order to get "MY_GENERATOR_SEQ".</p>
  
  <p><strong>Potential solutions:</strong></p>
  
  <ol>
  <li>set on SequenceGenerator allocationSize=1 (without setting hibernate.id.new_generator_mappings=false)</li>
  <li>create the sequence MY_GENERATOR_SEQ on the DB with INCREMENT BY 50 (without setting hibernate.id.new_generator_mappings=false)</li>
  </ol>
  
  <p>Both the solutions are not applicable into my context.
  So the only applicable "solution" is this one:
  Extend the Dialect used for Oracle and H2 and override the getNativeIdentifierGeneratorClass method in this way:</p>
  
  <pre><code>@Override
  public Class getNativeIdentifierGeneratorClass() {
      if ( getIdentityColumnSupport().supportsIdentityColumns() ) {
        return IdentityGenerator.class;
      }
      else {
          //From hibernate-core 5.0.7.Final, org.hibernate.dialect.Dialect
          //the original return value was org.hibernate.id.enhanced.SequenceStyleGenerator.class
        return SequenceGenerator.class;
      }
    }
  </code></pre>
  
  <p>and obviusly set the "databasePlatform" on org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter with the ah-hoc Dialect.</p>
  
  <p>Also submitted to hibernate comunity: <a href="https://hibernate.atlassian.net/browse/HHH-10656" rel="nofollow">https://hibernate.atlassian.net/browse/HHH-10656</a></p>