title: JPA/Hibernate5 get sequence nextval by sequence name
body: <p>How can I get <code>sequence nextval</code> in <code>JPA</code> or <code>Hibernate 5</code> by <code>sequence name</code>?</p>
  
  <p>I have sequence the following <code>TEST_SEQ</code> in <code>Oracle</code> DB and <code>ANOTHER_NAME_SEQ</code> in <code>Postgresql</code> DB.</p>
  
  <p>I need a method with following signature</p>
  
  <pre><code>public Long getSequenceByName(String sequenceName){}
  </code></pre>
  
  <p>And when I call this method it must return <code>nextval</code> from DB which is now used.</p>
  
  <p>I have a couple of ideas, but they are not suitable.</p>
  
  <p>1) Store native query for each DB in properties and write method like this:</p>
  
  <pre><code>@Value("${query}")//"SELECT {name}.NEXTVAL FROM DUAL"
  private StringQuery;
  
  public Long getSequenceByName(String sequenceName){
      uery q = em.createNativeQuery(StringQuery.replace("{name}", sequenceName));
      return (java.math.BigDecimal) q.getSingleResult();
    }
  </code></pre>
  
  <p>But I need to store the query string with placeholders and replace placeholder to sequence name, store query for each DB. </p>
  
  <p>2) Create entity with only one field <code>@Id</code>. Insert entity and getId(sequence value).</p>
  
  <p>But if in different DB is different sequence names - ???</p>
  
  <p>3) Use <a href="https://stackoverflow.com/a/23267212/4781349">this</a>. But It for hibernate 3 and I don't know if this is a good approach.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I try this solution:</p>
  
  <pre><code>@Component
  public class SequenseRepository {
  
      @PersistenceContext
      private EntityManager em;
  
      @Transactional
      public Long getID(final String sequenceName) {
          final List&lt;Long&gt; ids = new ArrayList&lt;&gt;(1);
  
          Session session = em.unwrap(Session.class);
          session.doWork(connection -&gt; {
              DialectResolver dialectResolver = new StandardDialectResolver();
              Dialect dialect =  dialectResolver.resolveDialect((DialectResolutionInfo) connection.getMetaData());
              PreparedStatement preparedStatement = null;
              ResultSet resultSet = null;
              try {
                  preparedStatement = connection.prepareStatement( dialect.getSequenceNextValString(sequenceName));
                  resultSet = preparedStatement.executeQuery();
                  resultSet.next();
                  ids.add(resultSet.getLong(1));
              }catch (SQLException e) {
                  throw e;
              } finally {
                  if(preparedStatement != null) {
                      preparedStatement.close();
                  }
                  if(resultSet != null) {
                      resultSet.close();
                  }
              }
          });
  
          return ids.get(0);
      }
  }
  </code></pre>
  
  <p>And I get exeption:</p>
  
  <pre><code>java.lang.ClassCastException: oracle.jdbc.driver.OracleDatabaseMetaData cannot be cast to org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo&amp;#xd;
  </code></pre>