To get a sequence nextval in oracle using Hibernate
where HibernateUtils is the following class
1 2 3 | public static BigDecimal getSequenceNextVal(String sequenceName) { return (BigDecimal)HibernateUtils.getSession().createSQLQuery( "select " +sequenceName+ ".NEXTVAL as id from dual" ).addScalar( "id" ).uniqueResult(); } |
1 |
1 | import java.util.ArrayList; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import tn.cnss.referentiel.models.Assure; import tn.cnss.referentiel.models.Beneficiaire; public class HibernateUtils { private static final SessionFactory sessionFactory; static { try { StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure( "hibernate.cfg.xml" ).build(); //Annotated classes to be scanned List<Class<?>> classes = new ArrayList<Class<?>>(); classes.add(YYYYYYYYYYYYY. class ); classes.add(XXXXXXXXXXXXX. class ); //Attach classes MetadataSources metadataSources = new MetadataSources(standardRegistry); for (Class<?> annotatedClass : classes) { metadataSources.addAnnotatedClass(annotatedClass); } sessionFactory = metadataSources.buildMetadata() .buildSessionFactory(); } catch (Throwable th) { System.err.println( "Enitial SessionFactory creation failed" + th); throw new ExceptionInInitializerError(th); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public static Session getSession() throws org.hibernate.HibernateException { SessionFactory sessionFactory = HibernateUtils.getSessionFactory(); Session sess = null ; try { sess = sessionFactory.getCurrentSession(); } catch (org.hibernate.HibernateException he) { sess = sessionFactory.openSession(); } return sess; } } |
1 |