title: How to use Oracle sequence with Hibernate + Spring
body: <p>Inside my entity class</p>
  
  <pre><code>     package com.entity;
  // Generated May 23, 2015 10:43:49 PM by Hibernate Tools 4.3.1
  
  
  import java.math.BigDecimal;
  import java.util.Date;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.SequenceGenerator;
  import javax.persistence.Table;
  import javax.persistence.Temporal;
  import javax.persistence.TemporalType;
  
  /**
   * EmpTest generated by hbm2java
   */
  @Entity
  @Table(name="EMP_TEST"
      ,schema="SCOTT"
  )
  public class EmpTest  implements java.io.Serializable {
  
  
       private BigDecimal id;
       private String ename;
       private Date dob;
       private BigDecimal sal;
  
      public EmpTest() {
      }
  
      public EmpTest(BigDecimal id, String ename, Date dob, BigDecimal sal) {
         this.id = id;
         this.ename = ename;
         this.dob = dob;
         this.sal = sal;
      }
  
       @Id 
  
       @GeneratedValue(strategy = GenerationType.SEQUENCE,
              generator = "test_seq")
      @SequenceGenerator(name = "test_seq",
              sequenceName = "TEST_SEQ" , allocationSize=1)  
      @Column(name="ID", unique=true, nullable=false, precision=22, scale=0)
      public BigDecimal getId() {
          return this.id;
      }
  
      public void setId(BigDecimal id) {
          this.id = id;
      }
  
  
      @Column(name="ENAME", nullable=false, length=50)
      public String getEname() {
          return this.ename;
      }
  
      public void setEname(String ename) {
          this.ename = ename;
      }
  
      @Temporal(TemporalType.DATE)
      @Column(name="DOB", nullable=false, length=7)
      public Date getDob() {
          return this.dob;
      }
  
      public void setDob(Date dob) {
          this.dob = dob;
      }
  
  
      @Column(name="SAL", nullable=false, precision=5)
      public BigDecimal getSal() {
          return this.sal;
      }
  
      public void setSal(BigDecimal sal) {
          this.sal = sal;
      }
  
  
  
  
  }
  </code></pre>
  
  <p><code>TEST_SEQ</code> is the sequence, which I created under my schema. But, when I'm trying to add record then getting following exception</p>
  
  <pre><code>Hibernate: select hibernate_sequence.nextval from dual
  Hibernate: select this_.ID as ID1_0_0_, this_.ENAME as ENAME2_0_0_, this_.DOB as DOB3_0_0_, this_.SAL as SAL4_0_0_ from SCOTT.EMP_TEST this_
      at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
      at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
      at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:89)
      at org.hibernate.id.SequenceGenerator.generateHolder(SequenceGenerator.java:122)
      at org.hibernate.id.SequenceGenerator.generate(SequenceGenerator.java:115)
      at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
      at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
      at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
      at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
      at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
      at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
      at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:711)
      at org.hibernate.internal.SessionImpl.save(SessionImpl.java:703)
      at org.hibernate.internal.SessionImpl.save(SessionImpl.java:698)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356)
      at com.sun.proxy.$Proxy318.save(Unknown Source)
      at com.model.EmpModelImp.add(EmpModelImp.java:32)
      at com.model.EmpServiceImpl.add(EmpServiceImpl.java:27)
      at com.controller.EmpControl.doActions(EmpControl.java:64)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:483)
      at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
      at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
      at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist
  
      at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:91)
      at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
      at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:206)
      at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:455)
      at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:413)
      at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:1034)
      at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:194)
      at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:791)
      at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:866)
      at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1186)
      at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3387)
      at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3431)
      at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1491)
      at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:80)
      ... 62 more
  </code></pre>
  
  <p>I have noticed that, It is trying to get next value from <code>hibernate_sequence</code> sequence, which doesn't exist in my database. On the other hand in my entity class I have pointed to <code>TEST_SEQ</code>, which exist in my database. I don't why It trying to get next sequence value from <code>hibernate_sequence</code>. Anyone please help me on this ?</p>
  
  <p><strong>==Update==</strong></p>
  
  <p><strong>EmpTest.hbm.xml</strong></p>
  
  <pre><code>&lt;hibernate-mapping&gt;
      &lt;class name="com.entity.EmpTest" table="EMP_TEST" schema="SCOTT" optimistic-lock="version"&gt;
          &lt;id name="id" type="big_decimal"&gt;
              &lt;column name="ID" precision="22" scale="0" /&gt;
              &lt;generator class="sequence" /&gt;
          &lt;/id&gt;
          &lt;property name="ename" type="string"&gt;
              &lt;column name="ENAME" length="50" not-null="true" /&gt;
          &lt;/property&gt;
          &lt;property name="dob" type="date"&gt;
              &lt;column name="DOB" length="7" not-null="true" /&gt;
          &lt;/property&gt;
          &lt;property name="sal" type="big_decimal"&gt;
              &lt;column name="SAL" precision="5" not-null="true" /&gt;
          &lt;/property&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Add function insert record into table</p>
  
  <pre><code> public void add(EmpTest emp) {
          try {
              session = HibernateUtil.getSessionFactory().getCurrentSession();
              tx = session.beginTransaction();
  
              System.out.println(emp.getEname()+" "+emp.getDob());
              session.save(emp);
              tx.commit();
          } catch (Exception ex) {
              tx.rollback();
              ex.printStackTrace();
          }
      }
  </code></pre>