title: Hibernate could not fetch the SequenceInformation from the database
body: <p>I have recently updated hibernate in my application to 5.4.4.Final. And now, I have faced with the following exception during deployment.</p>
  
  <pre><code>ERROR [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl|[STANDBY] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)']
  Could not fetch the SequenceInformation from the database
  java.sql.SQLException: Numeric Overflow
          at oracle.jdbc.driver.NumberCommonAccessor.throwOverflow(NumberCommonAccessor.java:4136)
          at oracle.jdbc.driver.NumberCommonAccessor.getLong(NumberCommonAccessor.java:634)
          at oracle.jdbc.driver.GeneratedStatement.getLong(GeneratedStatement.java:206)
          at oracle.jdbc.driver.GeneratedScrollableResultSet.getLong(GeneratedScrollableResultSet.java:259)
          at oracle.jdbc.driver.GeneratedResultSet.getLong(GeneratedResultSet.java:558)
          at weblogic.jdbc.wrapper.ResultSet_oracle_jdbc_driver_ForwardOnlyResultSet.getLong(Unknown Source)
          at org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorLegacyImpl.resultSetMaxValue(SequenceInformationExtractorLegacyImpl.java:139)
          at org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorLegacyImpl.extractMetadata(SequenceInformationExtractorLegacyImpl.java:61)
          at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl.sequenceInformationList(JdbcEnvironmentImpl.java:403)
          at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl.&lt;init&gt;(JdbcEnvironmentImpl.java:268)
          at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:114)
          at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
          at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101)
          at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
          at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
          at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
          at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152)
          at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286)
          at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243)
          at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
          at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.&lt;init&gt;(InFlightMetadataCollectorImpl.java:175)
          at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:118)
          at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:900)
          at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:931)
          at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:141)
          at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
          at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
          at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
          at com.sternkn.app.services.web.AppContextLoaderListener.&lt;clinit&gt;(AppContextLoaderListener.java:30)
  </code></pre>
  
  <p>I use the following persistence.xml.</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
     version="2.2"&gt;
  
     &lt;persistence-unit name="appPersistenceUnit" transaction-type="RESOURCE_LOCAL"&gt;
  
        &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
  
          &lt;properties&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.Oracle12cDialect" /&gt;
              &lt;property name="hibernate.id.new_generator_mappings" value="true"/&gt;
  
              &lt;property name="hibernate.cache.use_second_level_cache" value = "true"/&gt;
              &lt;property name="hibernate.cache.use_query_cache" value="false" /&gt;
              &lt;property name="hibernate.cache.region.factory_class" value="ehcache"/&gt;
              &lt;property name="hibernate.cache.ehcache.missing_cache_strategy" value="create" /&gt;
              &lt;property name="hibernate.cache.region_prefix" value="app_cache" /&gt;
              &lt;property name="net.sf.ehcache.configurationResourceName" value="/META-INF/app-ehcache.xml" /&gt;
              &lt;property name="hibernate.bytecode.provider" value="bytebuddy" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>After further investigation, I found out that the root cause is the following: hibernate uses the <strong>SequenceInformation</strong> interface for the sequences metadata manipulations</p>
  
  <pre class="lang-java prettyprint-override"><code>public interface SequenceInformation {
    Long getMinValue();
    Long getMaxValue();
    Long getIncrementValue();
    ...
  }
  </code></pre>
  
  <p>However, my app uses the sequences like the following:</p>
  
  <pre><code>SQL&gt; CREATE SEQUENCE SEQ_TEST START WITH 1 INCREMENT BY 1 NOCYCLE;
  SQL&gt; select MIN_VALUE, MAX_VALUE, INCREMENT_BY
  from USER_SEQUENCES
  where SEQUENCE_NAME = 'SEQ_TEST';
  
  MIN_VALUE MAX_VALUE                    INCREMENT_BY
  --------- ---------------------------- ------------
  1         9999999999999999999999999999 1
  </code></pre>
  
  <p>The <strong>Long.MAX_VALUE</strong> is equal to 9223372036854775807, therefore I got the numeric overflow exception.</p>
  
  <p>So, my questions:</p>
  
  <ul>
  <li>Is it a bug in hibernate?</li>
  <li>What will be the best way to solve it?</li>
  </ul>
  
  <p>Now I see the following ways:</p>
  
  <ol>
  <li>Fix the sequences declarations.
  It can be quite problematic in my case. And, by the way, it looks strange that hibernate tries to read metadata about all sequences, not only about used in my application.</li>
  <li>Create custom dialect that will extend Oracle12cDialect and override getQuerySequencesString() and/or getSequenceInformationExtractor().</li>
  </ol>
  
  <pre class="lang-java prettyprint-override"><code>public class Oracle8iDialect extends Dialect {
    ...
    public String getQuerySequencesString() {
      return "select * from all_sequences";
    }
  
    public SequenceInformationExtractor getSequenceInformationExtractor() {
      return SequenceInformationExtractorOracleDatabaseImpl.INSTANCE;
    }
  }
  </code></pre>
  
  <p>I can switch <strong>SequenceInformationExtractor</strong> to <strong>SequenceInformationExtractorNoOpImpl.INSTANCE</strong> and hibernate will not read sequences metadata. What impact will this decision have? Hibernate tries to validate <strong>allocationSize</strong> of @SequenceGenerator() by INCREMENT_BY. Are there other reasons?</p>
  
  <p>Any suggestions will be appreciated.</p>
  
  <p><strong>UPDATE</strong>: This is <a href="https://hibernate.atlassian.net/browse/HHH-13694" rel="noreferrer">HHH-13694</a></p>