title: How to create a sequence in JPA without creating it directly in the database
body: <p>I am doing a service that gets data from a source and fetches them into my Database by using JPA. The id is generated by using sequence.
  I have created the sequence on my DB by using this command:</p>
  <pre><code>CREATE SEQUENCE crm_test_sq_1 MINVALUE 1 MAXVALUE 999999999999999999999999999 START WITH 1 INCREMENT BY 1 CACHE 100 NOCYCLE
  </code></pre>
  <p>However, I don't know how to create the sequence directly from my code.</p>
  <p>Can anyone please help me to figure out this issue? Thank you so much!</p>
  <p>My model class, modeltest.java:</p>
  <pre><code>    @Entity
      @Table(name = &quot;MODEL_TEST&quot;)
      
      @JsonPropertyOrder({ &quot;ID&quot;, &quot;CODE&quot;, &quot;NAME&quot; })
      @XmlRootElement
      @NamedQueries({
          @NamedQuery(name = &quot;modeltest.getall&quot;, query = &quot;SELECT e FROM modelteste&quot;),
          @NamedQuery(name = &quot;modeltest.deleteAll&quot;, query = &quot;DELETE FROM modeltest e&quot;)})
      public class modeltest implements Serializable {
          private static final long serialVersionUID = 1L;
      
          @Id
          @GeneratedValue(generator = &quot;crm_test_sq&quot;, strategy = GenerationType.SEQUENCE)
          @SequenceGenerator(name = &quot;crm_test_sq&quot;, sequenceName = &quot;crm_test_sq&quot;,allocationSize=1)
          @Column(name=&quot;ID&quot;, unique=true, nullable=false, precision=10, scale=0)
          private Long id;
      
          @Column(name = &quot;CODE&quot;)
          private String isocurrencycode;
      
          @Column(name = &quot;NAME&quot;)
          private String currencyname;
      
          public modeltest() {
          }
  //........... getter and setter method
  </code></pre>
  <p>My dao class, modeltestDao.java</p>
  <pre><code>public Boolean saveData() {
          
          
  //............................................
              getEntityManager().createNamedQuery(&quot;modeltest.deleteAll&quot;).executeUpdate();
              
              // i want something like below, but obviously this line of code give me an error
              getEntityManager().createNativeQuery (&quot;CREATE SEQUENCE crm_test_sq_1 MINVALUE 1 MAXVALUE 999999999999999999999999999 START WITH 1 INCREMENT BY 1 CACHE 100 NOCYCLE&quot;).executeUpdate();
              
          /....................
          return true;
      }
  </code></pre>
  <p>EDIT: Btw, why i suppose to reset the sequence is each time i run the service (fetch data into database), the unique identifiers has increased by 1. For example, first time i run data, the id start with 1 and end with 10, second time i run data, the id start with 11 and end with 20. This problem only stop when i restart the server.</p>
  <pre><code>For example:
  call saveData() first time
  DATA IN TABLE:
  ID 1 2 3 4 5
  call saveData() second time
  DATA IN TABLE:
  ID 6 7 8 9 10
  </code></pre>
  <p>Other data do not change, but id. So i find a way to solve this is drop and recreate sequence each time i run saveData()</p>