# AUTOINCREMENTABLE SECUENCE

Podemos generar campos autoincrementable mediante SECUENCE en OracleDB.

```sql
CREATE SEQUENCE TODOX_SEQ START WITH 1000 INCREMENT BY  1 NOCACHE  NOCYCLE;


```

Crear la table indicando la secuencia.

```sql
  CREATE TABLE TODO (
    TODOID NUMBER DEFAULT TODOX_SEQ.nextval NOT NULL,
    DESCRIPCION VARCHAR2(200)  ,
      CONSTRAINT TODOPK  PRIMARY KEY (TODOID)
    );
```

Definir la entidad

```sql
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;

/**
 *
 * @author avbravo
 */
@Entity
public class Todo implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @SequenceGenerator(name = "TODOX_GEN", sequenceName = "TODOX_SEQ")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TODOX_GEN")
    private Integer TODOID;
    private String DESCRIPCION;

    public Integer getTODOID() {
        return TODOID;
    }

    public void setTODOID(Integer TODOID) {
        this.TODOID = TODOID;
    }

    public String getDESCRIPCION() {
        return DESCRIPCION;
    }

    public void setDESCRIPCION(String DESCRIPCION) {
        this.DESCRIPCION = DESCRIPCION;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (TODOID != null ? TODOID.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // Todo: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Todo)) {
            return false;
        }
        Todo other = (Todo) object;
        if ((this.TODOID == null && other.TODOID != null)
                || (this.TODOID != null && !this.TODOID.equals(other.TODOID))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.peopleinmotion.horizonreinicioremotoejb.entity.TODO[ id=" + TODOID + " ]";
    }

}

```

Definir el repository

```java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.peopleinmotion.horizonreinicioremotoejb.repository;

import com.peopleinmotion.horizonreinicioremotoejb.entity.Todo;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author avbravo
 */
@Stateless
public class TodoFacade extends AbstractFacade<Todo> {

    @PersistenceContext(unitName = "com.people-inmotion_horizonreinicioremotoejb_ejb_1.0-SNAPSHOTPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public TodoFacade() {
        super(Todo.class);
    }
    
    public List<Todo> findByDescripcion(String descripcion) {
        Query query = em.createNamedQuery("PEOPLE.findByDescripcion");
        return query.setParameter("descripcion",descripcion).getResultList();
    }

   

  
    
}

```

En el Controller

```java
 public String saveTodo() {
        try {

            Todo todo = new Todo();
            todo.setDESCRIPCION("descripcion");

            todoFacade.create(todo);

            JsfUtil.successMessage("tODO guardardo.....");

        } catch (ConstraintViolationException e) {
            System.out.println("ConstraintViolationException " + e.getLocalizedMessage());
            JsfUtil.errorMessage("eonstraintViolationException ()) " + e.getLocalizedMessage());

        } catch (Exception e) {
            System.out.println("saveTodo()) " + e.getLocalizedMessage());
            JsfUtil.errorMessage("saveTodo() " + e.getLocalizedMessage());
        }
        return "";
    }
```
