Get the next value from SequenceGenerator

Avin picture Avin · Jul 12, 2012 · Viewed 32.6k times · Source

I'd like to make use of SequenceGenerator and get the next value from the sequence generator. I want to avoid making a trip to the DB and rely on the sequencegenerator to give me a unique value. Also I am using the value to display the unique id in a display field without persisting the object.

Answer

staverk picture staverk · Dec 13, 2015

It works in Oracle:

Query q = em.createNativeQuery("SELECT nombreSecuencia.NEXTVAL FROM DUAL");

return (java.math.BigDecimal)q.getSingleResult();