title: Custom IdentifierGenerator
body: <p>I am currently facing a problem that makes me crazy.</p>
  
  <p>I would like to create my own IdentifierGenerator to manage ids with a specific format.</p>
  
  <pre><code>public class CustomGenerator implements IdentifierGenerator {
  
      private static final String REQUEST = "SELECT CURRVAL('sequence')";
  
      private final String prefixFormat = "PREFIX-%s";
  
      @Override
      public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
          final String prefix = getPrefix();
          Session sessionHibernate = (Session) session;
          Query query = sessionHibernate.createSQLQuery(REQUEST );
          Object obj = query.uniqueResult();
          return null;
      }
  
      private String getPrefix() {
          final Calendar date = Calendar.getInstance();
          DateFormat dateFormat = new SimpleDateFormat("yyyyMM");
          return String.format(prefixFormat, dateFormat.format(date.getTime()));
      }
  
  }
  </code></pre>
  
  <p>My Junit Test:</p>
  
  <pre><code>@Test
  @Transactional
  public void addEntity() {
      final Entity entity= new Entity();
      final long count = this.entityRepository.count();
  
      this.entityRepository.saveAndFlush(rex);
  
      assertEquals(count + 1, this.entityRepository.count());
  }
  </code></pre>
  
  <p>My Entity:</p>
  
  <pre><code>@Entity
  @Table(name = "ENTITY")
  public class Entity {
  
      @Id
      @GenericGenerator(name = "sequence", strategy = "[...]CustomGenerator")
      @GeneratedValue(generator = "sequence")
      @Column(name = "ENTITY_ID")
      private Integer entityId;
  
      [...]
  }
  </code></pre>
  
  <p>My JUnit test crashes  - Stacktrace:</p>
  
  <pre><code>org.springframework.dao.InvalidDataAccessResourceUsageException: could not  prepare statement; SQL [SELECT CURRVAL('sequence_rex_id')]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:238)
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:221)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
      at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
      at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
  </code></pre>
  
  <p>Database: HSQLDB (test), POSTGRESQL (production)</p>
  
  <p>Does anyone have any idea ?</p>
  
  <p>Sorry for any mistakes I could have made. English is not my mother tongue.</p>