title: SequenceGenerator to SequenceStyleGenerator moving from hibernate 4.2 to 5
body: <p>I've recently upgraded my project to use hibernate 5 which was earlier using hibernate 4.2. We have database sequence with naming convention followed as "SEQ_PrimaryKeyName". In hibernate 4.2 we were using org.hibernate.id.SequenceGenerator to generate the sequence - the code looks like this -</p>
  
  <pre><code>public class PrimaryKeyGenerator extends IdentityGenerator implements      Configurable {
  
      private PrimaryKeyGenerator  pkGen;
  
      public PrimaryKeyGenerator  () {
          pkGen= new SequenceGenerator();
      }
  
      //configure sequence generator 
      public void configure(Type type, Properties params, Dialect Dialect) throws MappingException {
          if (pkGen instanceof Configurable) {
              String seqName = "SEQ_" + params.getProperty(PersistentIdentifierGenerator.PK);
              params.setProperty(SequenceGenerator.SEQUENCE, seqName);    
              ((Configurable)pkGen).configure(type, params, dialect);
          }
      }
  
      // Generate sequence
      public Serializable generate(SessionImplementor session, Object obj) throws HibernateException {
          return pkGen.generate(session, obj);
      }
  }
  </code></pre>
  
  <p>SequenceGenerator is deprecated in hibernate 5 and as per javadoc it is recommended to use org.hibernate.id.enhanced.SequenceStyleGenerator.</p>
  
  <p>I modified my existing PrimaryKeyGenerator class to following </p>
  
  <pre><code> public class PrimaryKeyGenerator extends IdentityGenerator implements Configurable {
       private PrimaryKeyGenerator pkGen;
  
       public PrimaryKeyGenerator() {
           pkGen = new SequenceStyleGenerator();
       }
  
      // implement the methods of SequenceStyleGenerator.java
      @Override
      public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
          if (pkGen instanceof Configurable) {
              String seqName = "SEQ_" + params.getProperty(PersistentIdentifierGenerator.PK);
              params.setProperty(SequenceStyleGenerator.SEQUENCE_PARAM, seqName);
              ((Configurable) pkGen).configure(type, params, serviceRegistry);
          }
      }
  
      public Serializable generate(SessionImplementor session, Object obj) throws HibernateException {
  
          // collect instance of org.hibernate.boot.model.relational.Database   
          Database db = MetadataExtractor.INSTANCE.getDatabase();
          (SequenceStyleGenerator)pkGen).registerExportables(db);
          return pkGen.generate(session, obj);
      }
  }
  </code></pre>
  
  <p>With above changes sequence are getting generated properly. The one question I have is that Is it allright to call registerExportable() method before calling generate method ? I am not sure but it looks like registerExportable should be called only once and not every time time generate() method is called. If I don't make a call to registerExportable method explicitly I get following exception.</p>
  
  <p><code>"SequenceStyleGenerator</code>'s SequenceStructure was not properly initialized"</p>
  
  <p>May be the way I am trying to use SequenceStyleGenerator is not correct.</p>